C# WinForm, subclass a Checkbox control to create a better two-state button (Adds Checkbox within a Button)


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

See comments in code.


Copy this code and paste it in your HTML
  1. // ToggleButton
  2. // This class subclasses a CheckBox to combine the benefits of a Checkbox and
  3. // Checkbox-with-Button-Appearance by combining a checkbox and button into one control.
  4. //
  5. // Requirements:
  6. // [1] Two states (like a checkbox).
  7. // [2] Visually imply action/control (like a button).
  8. // I think a Checkbox fails this requirement.
  9. // [3] It should be intuitively obvious what the state is and what the button will do when clicked.
  10. // A checkbox meets the state requirement but fails the Action requirement because
  11. // checkboxes are typically used for configuration rather than action.
  12. // I think a Checkbox-with-button-appearance fails because it's not obvious if the button is depressed or not.
  13. // I think a button-with-changing-text fails. Does the text show state or the on-click (oposite) action?
  14. // Designers use button-with-changing-text inconsistently.
  15. // [4] Explicity show State even with poor dispplay visibility.
  16. // I think a Checkbox-with-button-appearance fails this requirement.
  17. // [5] Works on all resolutions and themes, doesn't look to 'flashy' and must 'fit-in'
  18. // I think a 2-bitmapped button would fail this requirement.
  19. // Many 3-rd party buttons fail this requirement.
  20. //
  21. // This class uses DrawToBitmap to capture a temporary Checkbox's appearance.
  22. // It captures just the checkbox without the text.
  23. // I want to use a standard checkbox to be consistent with system-wide styles.
  24. //
  25. // Design usage:
  26. // [1] Add this class ToggleButton to project.
  27. // [2] Add 'normal' checkbox to WinForm
  28. // [3] Change it's class to this ToggleButton class (in both the declaration and instantiation).
  29. // [4] Set properties...
  30. // Appearance = System.Windows.Forms.Appearance.Button;
  31. // TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText;
  32. //
  33. // Room for improvement:
  34. // Create a user control that's more portable (although it's easy to add a class to a project).
  35. public class ToggleButton : CheckBox
  36. {
  37. //constructor
  38. public ToggleButton() : base()
  39. {
  40. this.SuspendLayout();
  41.  
  42. if (bmChecked == null) // If the static bitmaps haven't been constructed, create them.
  43. {
  44. System.Windows.Forms.CheckBox chkTemp = new System.Windows.Forms.CheckBox();
  45.  
  46. chkTemp.AutoSize = true;
  47. chkTemp.BackColor = System.Drawing.Color.Transparent;
  48. chkTemp.Location = new System.Drawing.Point(123, 128);
  49. chkTemp.Name = "chkSmall";
  50. chkTemp.Size = chkTemp.PreferredSize;
  51. chkTemp.TabIndex = 2;
  52. chkTemp.UseVisualStyleBackColor = false;
  53.  
  54. bmChecked = new Bitmap(chkTemp.Width, chkTemp.Height);
  55. bmUnChecked = new Bitmap(chkTemp.Width, chkTemp.Height);
  56. bmDisabled = new Bitmap(chkTemp.Width, chkTemp.Height);
  57.  
  58. // Set checkbox false and capture bitmap.
  59. chkTemp.Checked = false;
  60. chkTemp.DrawToBitmap(bmUnChecked, new Rectangle(0, 0, chkTemp.Width, chkTemp.Height));
  61.  
  62. // Set checkbox true and capture bitmap.
  63. chkTemp.Checked = true;
  64. chkTemp.DrawToBitmap(bmChecked, new Rectangle(0, 0, chkTemp.Width, chkTemp.Height));
  65.  
  66. // Set checkbox false/disabled and capture bitmap.
  67. chkTemp.Checked = false;
  68. chkTemp.Enabled = false;
  69. chkTemp.DrawToBitmap(bmDisabled, new Rectangle(0, 0, chkTemp.Width, chkTemp.Height));
  70.  
  71. chkTemp.Visible = false;
  72.  
  73. }
  74. this.CheckedChanged += new System.EventHandler(this.btnToggleButton_Changed);
  75. this.EnabledChanged += new System.EventHandler(this.btnToggleButton_Changed);
  76. this.Image = this.Checked ? bmChecked : bmUnChecked;
  77. this.ResumeLayout(false);
  78. btnToggleButton_Changed(null, null);
  79. }
  80.  
  81.  
  82. private void btnToggleButton_Changed(object sender, EventArgs e)
  83. {
  84. Image =
  85. !Enabled ? bmDisabled :
  86. Checked ? bmChecked :
  87. bmUnChecked;
  88. }
  89.  
  90. // Create 3 static bitmaps to swap-out when the button changes.
  91. // Static saves space (we don't need a copy for each instance of this class) but it seemed to break the design-time display.
  92. private static Bitmap bmChecked;
  93. private static Bitmap bmUnChecked;
  94. private static Bitmap bmDisabled;
  95.  
  96. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.