Return to Snippet

Revision: 30115
at August 8, 2010 02:35 by maxbaines


Initial Code
public class ToggleButton : System.Windows.Forms.CheckBox
{
   // 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:
   //    There's probably a way to automatically change Image based on state. I don't know yet.
   //    Create a user control that's more portable (although it's easy to add a class to a project).
   //    Consider making the bitmaps static merely to save space.
 
   System.Drawing.Bitmap bmChecked;
   System.Drawing.Bitmap bmUnChecked;
 
   //constructor
   public ToggleButton() : base() 
   {
 
      System.Windows.Forms.CheckBox chkTemp = new System.Windows.Forms.CheckBox(); // Temporary checkbox with no text.
 
      chkTemp.AutoSize = true;
      chkTemp.BackColor = System.Drawing.Color.Transparent;
      chkTemp.Size = new System.Drawing.Size(15, 14);
      chkTemp.UseVisualStyleBackColor = false;
 
      bmChecked = new System.Drawing.Bitmap(chkTemp.Width, chkTemp.Height);
      bmUnChecked = new System.Drawing.Bitmap(chkTemp.Width, chkTemp.Height);
 
      // Set checkbox false and capture bitmap.
      chkTemp.Checked = false; 
      chkTemp.DrawToBitmap(bmUnChecked, new System.Drawing.Rectangle(0, 0, chkTemp.Width, chkTemp.Height));
 
      // Set checkbox true and capture bitmap.
      chkTemp.Checked = true;
      chkTemp.DrawToBitmap(bmChecked, new System.Drawing.Rectangle(0, 0, chkTemp.Width, chkTemp.Height));
 
      this.CheckedChanged += new System.EventHandler(this.btnToggleButton_CheckedChanged);
 
      this.Image = this.Checked ? bmChecked : bmUnChecked;
 
   }
 
   // Change the this.Image
   private void btnToggleButton_CheckedChanged(object sender, System.EventArgs e)
   {
      this.Image = this.Checked ? bmChecked : bmUnChecked;
   }
 
}

Initial URL

                                

Initial Description
comp of  another snippet

Initial Title
CheckBox Button

Initial Tags

                                

Initial Language
C#