CheckedChanged event for CheckBox : CheckBox « GUI Windows Forms « C# / CSharp Tutorial





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);
     }
}










23.9.CheckBox
23.9.1.Change Label font by CheckBoxesChange Label font by CheckBoxes
23.9.2.Add CheckBox to a FormAdd CheckBox to a Form
23.9.3.CheckedChanged event for CheckBox