Mega Code Archive

 
Categories / C# Tutorial / GUI Windows Forms
 

CheckedChanged event for CheckBox

using System; using System.Drawing; using System.Windows.Forms;     class CheckBoxDemo: Form {      public static void Main()      {           Application.Run(new CheckBoxDemo());      }      public CheckBoxDemo()      {           CheckBox[] achkbox  = new CheckBox[4];           int        cyText   = Font.Height;           int        cxText   = cyText / 2;           string[]   astrText = {"Bold", "Italic", "Underline", "Strikeout"};               for (int i = 0; i < 4; i++){                achkbox[i] = new CheckBox();                achkbox[i].Text = astrText[i];                achkbox[i].Location = new Point(2 * cxText, (4 + 3 * i) * cyText / 2);                achkbox[i].Size = new Size(12 * cxText, cyText);                achkbox[i].CheckedChanged += new EventHandler(CheckBoxOnCheckedChanged);           }           Controls.AddRange(achkbox);      }      void CheckBoxOnCheckedChanged(object obj, EventArgs ea)      {           Invalidate(false);      }      FontStyle[] afs  = { FontStyle.Bold,FontStyle.Italic, FontStyle.Underline, FontStyle.Strikeout };      FontStyle   fs   = 0;           protected override void OnPaint(PaintEventArgs pea)      {           Graphics    grfx = pea.Graphics;           for (int i = 0; i < 4; i++){                if (((CheckBox) Controls[i]).Checked){                     fs |= afs[i];                }           }           grfx.DrawString(Text, new Font(Font, fs), new SolidBrush(ForeColor), 0, 0);      } }