CheckBox Button


/ Published in: C#
Save to your folder(s)

comp of another snippet


Copy this code and paste it in your HTML
  1. public class ToggleButton : System.Windows.Forms.CheckBox
  2. {
  3. // This class subclasses a CheckBox to combine the benefits of a Checkbox and
  4. // Checkbox-with-Button-Appearance by combining a checkbox and button into one control.
  5. //
  6. // Requirements:
  7. // [1] Two states (like a checkbox).
  8. // [2] Visually imply action/control (like a button).
  9. // I think a Checkbox fails this requirement.
  10. // [3] It should be intuitively obvious what the state is and what the button will do when clicked.
  11. // A checkbox meets the state requirement but fails the Action requirement because
  12. // checkboxes are typically used for configuration rather than action.
  13. // I think a Checkbox-with-button-appearance fails because it's not obvious if the button is depressed or not.
  14. // I think a button-with-changing-text fails. Does the text show state or the on-click (oposite) action?
  15. // Designers use button-with-changing-text inconsistently.
  16. // [4] Explicity show State even with poor dispplay visibility.
  17. // I think a Checkbox-with-button-appearance fails this requirement.
  18. // [5] Works on all resolutions and themes, doesn't look to 'flashy' and must 'fit-in'
  19. // I think a 2-bitmapped button would fail this requirement.
  20. // Many 3-rd party buttons fail this requirement.
  21. //
  22. // This class uses DrawToBitmap to capture a temporary Checkbox's appearance.
  23. // It captures just the checkbox without the text.
  24. // I want to use a standard checkbox to be consistent with system-wide styles.
  25. //
  26. // Design usage:
  27. // [1] Add this class ToggleButton to project.
  28. // [2] Add 'normal' checkbox to WinForm
  29. // [3] Change it's class to this ToggleButton class (in both the declaration and instantiation).
  30. // [4] Set properties...
  31. // Appearance = System.Windows.Forms.Appearance.Button;
  32. // TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText;
  33. //
  34. // Room for improvement:
  35. // There's probably a way to automatically change Image based on state. I don't know yet.
  36. // Create a user control that's more portable (although it's easy to add a class to a project).
  37. // Consider making the bitmaps static merely to save space.
  38.  
  39. System.Drawing.Bitmap bmChecked;
  40. System.Drawing.Bitmap bmUnChecked;
  41.  
  42. //constructor
  43. public ToggleButton() : base()
  44. {
  45.  
  46. System.Windows.Forms.CheckBox chkTemp = new System.Windows.Forms.CheckBox(); // Temporary checkbox with no text.
  47.  
  48. chkTemp.AutoSize = true;
  49. chkTemp.BackColor = System.Drawing.Color.Transparent;
  50. chkTemp.Size = new System.Drawing.Size(15, 14);
  51. chkTemp.UseVisualStyleBackColor = false;
  52.  
  53. bmChecked = new System.Drawing.Bitmap(chkTemp.Width, chkTemp.Height);
  54. bmUnChecked = new System.Drawing.Bitmap(chkTemp.Width, chkTemp.Height);
  55.  
  56. // Set checkbox false and capture bitmap.
  57. chkTemp.Checked = false;
  58. chkTemp.DrawToBitmap(bmUnChecked, new System.Drawing.Rectangle(0, 0, chkTemp.Width, chkTemp.Height));
  59.  
  60. // Set checkbox true and capture bitmap.
  61. chkTemp.Checked = true;
  62. chkTemp.DrawToBitmap(bmChecked, new System.Drawing.Rectangle(0, 0, chkTemp.Width, chkTemp.Height));
  63.  
  64. this.CheckedChanged += new System.EventHandler(this.btnToggleButton_CheckedChanged);
  65.  
  66. this.Image = this.Checked ? bmChecked : bmUnChecked;
  67.  
  68. }
  69.  
  70. // Change the this.Image
  71. private void btnToggleButton_CheckedChanged(object sender, System.EventArgs e)
  72. {
  73. this.Image = this.Checked ? bmChecked : bmUnChecked;
  74. }
  75.  
  76. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.