/ Published in: C#
See comments in code.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// ToggleButton // This class subclasses a CheckBox to combine the benefits of a Checkbox and // Checkbox-with-Button-Appearance by combining a checkbox and button into one control. // // Requirements: // [1] Two states (like a checkbox). // [2] Visually imply action/control (like a button). // I think a Checkbox fails this requirement. // [3] It should be intuitively obvious what the state is and what the button will do when clicked. // A checkbox meets the state requirement but fails the Action requirement because // checkboxes are typically used for configuration rather than action. // I think a Checkbox-with-button-appearance fails because it's not obvious if the button is depressed or not. // I think a button-with-changing-text fails. Does the text show state or the on-click (oposite) action? // Designers use button-with-changing-text inconsistently. // [4] Explicity show State even with poor dispplay visibility. // I think a Checkbox-with-button-appearance fails this requirement. // [5] Works on all resolutions and themes, doesn't look to 'flashy' and must 'fit-in' // I think a 2-bitmapped button would fail this requirement. // Many 3-rd party buttons fail this requirement. // // This class uses DrawToBitmap to capture a temporary Checkbox's appearance. // It captures just the checkbox without the text. // I want to use a standard checkbox to be consistent with system-wide styles. // // Design usage: // [1] Add this class ToggleButton to project. // [2] Add 'normal' checkbox to WinForm // [3] Change it's class to this ToggleButton class (in both the declaration and instantiation). // [4] Set properties... // Appearance = System.Windows.Forms.Appearance.Button; // TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText; // // Room for improvement: // Create a user control that's more portable (although it's easy to add a class to a project). public class ToggleButton : CheckBox { //constructor public ToggleButton() : base() { this.SuspendLayout(); if (bmChecked == null) // If the static bitmaps haven't been constructed, create them. { chkTemp.AutoSize = true; chkTemp.BackColor = System.Drawing.Color.Transparent; chkTemp.Name = "chkSmall"; chkTemp.Size = chkTemp.PreferredSize; chkTemp.TabIndex = 2; chkTemp.UseVisualStyleBackColor = false; // Set checkbox false and capture bitmap. // Set checkbox true and capture bitmap. // Set checkbox false/disabled and capture bitmap. chkTemp.Enabled = false; chkTemp.Visible = false; } this.ResumeLayout(false); btnToggleButton_Changed(null, null); } private void btnToggleButton_Changed(object sender, EventArgs e) { Image = !Enabled ? bmDisabled : bmUnChecked; } // Create 3 static bitmaps to swap-out when the button changes. // Static saves space (we don't need a copy for each instance of this class) but it seemed to break the design-time display. private static Bitmap bmChecked; private static Bitmap bmUnChecked; private static Bitmap bmDisabled; }