ComboBox selected Item changed event 1

   


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form
{
  private System.Windows.Forms.GroupBox groupBox1;
  private System.Windows.Forms.ComboBox lstFonts;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.StatusStrip statusBar;
  private System.Windows.Forms.ToolStripStatusLabel statusLabel;

  public Form1() {
        InitializeComponent();
    System.Drawing.Text.InstalledFontCollection fonts = new System.Drawing.Text.InstalledFontCollection();
    foreach (FontFamily family in fonts.Families)
    {
      lstFonts.Items.Add(family.Name);
    }
  }

  private void lstFonts_SelectedIndexChanged(object sender, EventArgs e)
  {
        this.Invalidate();
  }

  private void Form1_Paint(object sender, PaintEventArgs e)
  {
    if (lstFonts.SelectedIndex != -1)
    {
            e.Graphics.DrawString(lstFonts.Text, new Font(lstFonts.Text, 50), Brushes.Black, 10, 50);
            statusBar.Items[0].Text = lstFonts.Text;
    }
  }

  private void InitializeComponent()
  {
    this.groupBox1 = new System.Windows.Forms.GroupBox();
    this.lstFonts = new System.Windows.Forms.ComboBox();
    this.label1 = new System.Windows.Forms.Label();
    this.statusBar = new System.Windows.Forms.StatusStrip();
    this.statusLabel = new System.Windows.Forms.ToolStripStatusLabel();
    this.groupBox1.SuspendLayout();
    this.statusBar.SuspendLayout();
    this.SuspendLayout();
    // 
    // groupBox1
    // 
    this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
          | System.Windows.Forms.AnchorStyles.Right)));
    this.groupBox1.Controls.Add(this.lstFonts);
    this.groupBox1.Controls.Add(this.label1);
    this.groupBox1.Location = new System.Drawing.Point(7, 0);
    this.groupBox1.Name = "groupBox1";
    this.groupBox1.Size = new System.Drawing.Size(497, 40);
    this.groupBox1.TabIndex = 1;
    this.groupBox1.TabStop = false;
    // 
    // lstFonts
    // 
    this.lstFonts.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    this.lstFonts.DropDownWidth = 340;
    this.lstFonts.FormattingEnabled = true;
    this.lstFonts.Location = new System.Drawing.Point(100, 12);
    this.lstFonts.Name = "lstFonts";
    this.lstFonts.Size = new System.Drawing.Size(340, 21);
    this.lstFonts.TabIndex = 1;
    this.lstFonts.SelectedIndexChanged += new System.EventHandler(this.lstFonts_SelectedIndexChanged);
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(12, 16);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(80, 12);
    this.label1.TabIndex = 0;
    this.label1.Text = "Choose Font:";
    // 
    // statusBar
    // 
    this.statusBar.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.statusLabel});
    this.statusBar.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.Table;
    this.statusBar.Location = new System.Drawing.Point(0, 155);
    this.statusBar.Name = "statusBar";
    this.statusBar.Size = new System.Drawing.Size(516, 22);
    this.statusBar.TabIndex = 2;
    this.statusBar.Text = "statusStrip1";
    // 
    // statusLabel
    // 
    this.statusLabel.Name = "statusLabel";
    // 
    // Form1
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(516, 177);
    this.Controls.Add(this.groupBox1);
    this.Controls.Add(this.statusBar);
    this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.Text = "Font Viewer";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.groupBox1.ResumeLayout(false);
    this.statusBar.ResumeLayout(false);
    this.ResumeLayout(false);
    this.PerformLayout();

  }

  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
  }

}


           
          


ComboBox with color cell renderer



   

/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury, 
   Zach Greenvoss, Shripad Kulkarni, Neil Whitlow

Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace MyComboBox
{
  /// <summary>
  /// Summary description for MyComboBox.
  /// </summary>
  public class MyComboBox : System.Windows.Forms.Form
  {
    private System.Windows.Forms.ComboBox comboBox1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.ComboBox comboBox2;
    private System.Windows.Forms.Label label2;
    ArrayList colorArray = new ArrayList() ;
    ArrayList fontArray  = new ArrayList() ;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    public MyComboBox()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

      //
      // TODO: Add any constructor code after InitializeComponent call
      //
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null) 
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.comboBox1 = new System.Windows.Forms.ComboBox();
      this.label1 = new System.Windows.Forms.Label();
      this.comboBox2 = new System.Windows.Forms.ComboBox();
      this.label2 = new System.Windows.Forms.Label();
      this.SuspendLayout();
      // 
      // comboBox1
      // 
      this.comboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
      this.comboBox1.ItemHeight = 25;
      this.comboBox1.Location = new System.Drawing.Point(16, 40);
      this.comboBox1.Name = "comboBox1";
      this.comboBox1.Size = new System.Drawing.Size(264, 31);
      this.comboBox1.TabIndex = 0;
      this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.MyItemSelected);
      this.comboBox1.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.comboBox1_MeasureItem);
      this.comboBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.comboBox1_DrawItem);
      // 
      // label1
      // 
      this.label1.Location = new System.Drawing.Point(16, 16);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(100, 16);
      this.label1.TabIndex = 1;
      this.label1.Text = "Font Combo Box";
      // 
      // comboBox2
      // 
      this.comboBox2.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
      this.comboBox2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
      this.comboBox2.ItemHeight = 20;
      this.comboBox2.Location = new System.Drawing.Point(16, 104);
      this.comboBox2.Name = "comboBox2";
      this.comboBox2.Size = new System.Drawing.Size(264, 26);
      this.comboBox2.TabIndex = 0;
      this.comboBox2.SelectedIndexChanged += new System.EventHandler(this.MyItemSelected);
      this.comboBox2.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.comboBox2_DrawItem);
      // 
      // label2
      // 
      this.label2.Location = new System.Drawing.Point(24, 80);
      this.label2.Name = "label2";
      this.label2.TabIndex = 2;
      this.label2.Text = "Color Combo Box";
      // 
      // MyComboBox
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(312, 157);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.label2,
                                      this.label1,
                                      this.comboBox1,
                                      this.comboBox2});
      this.Name = "MyComboBox";
      this.Text = "OwnerDraw ComboBox";
      this.Load += new System.EventHandler(this.MyComboBox_Load);
      this.ResumeLayout(false);

    }
    #endregion

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
      Application.Run(new MyComboBox());
    }

    private void MyComboBox_Load(object sender, System.EventArgs e)
    {  
      // Fill an array with the different color you wish
      // to display in the ComboBox

      colorArray.Add(new SolidBrush(Color.Yellow));
      colorArray.Add(new SolidBrush(Color.Black));
      colorArray.Add(new SolidBrush(Color.Azure));
      colorArray.Add(new SolidBrush(Color.Firebrick));
      colorArray.Add(new SolidBrush(Color.DarkMagenta));

      // Add blank items to the list, since the text we will display
      // will be the name of the Color we are painting
      comboBox2.Items.Add("");
      comboBox2.Items.Add("");
      comboBox2.Items.Add("");
      comboBox2.Items.Add("");
      comboBox2.Items.Add("");

    
      // Fill an array with the different fonts that you will use to display 
      // items in the other comboBox

      fontArray .Add(new Font("Ariel" , 15 , FontStyle.Bold ));
      fontArray .Add(new Font("Courier" , 12 , FontStyle.Italic));
      fontArray .Add(new Font("Veranda" , 14 , FontStyle.Bold));
      fontArray .Add(new Font("System" , 10 , FontStyle.Strikeout));
      fontArray .Add(new Font("Century SchoolBook" , 15 , FontStyle.Underline));

      // Add the items that in the listBox

      comboBox1.Items.Add("Washington");
      comboBox1.Items.Add("Houston");
      comboBox1.Items.Add("Phoenix");
      comboBox1.Items.Add("Dallas");
      comboBox1.Items.Add("London");

    }

    private void comboBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
    {
      // Override this function to draw items in the Font comboBox

      // Get the Graphics Object (aka. CDC or Device Context Object )
      // passed via the DrawItemEventArgs parameter
      Graphics g = e.Graphics ;

      // Get the bounding rectangle of the item currently being painted
      Rectangle r = e.Bounds ;
      Font fn = null ;

      if ( e.Index >= 0 ) 
      {
        // Get the Font object, at the specifid index in the fontArray
        fn = (Font)fontArray[e.Index];

        // Get the text that we wish to display
        string s = (string)comboBox1.Items[e.Index];

        // Set the string format options
        StringFormat sf = new StringFormat();
        sf.Alignment = StringAlignment.Near;

        Console.WriteLine(e.State.ToString());
        // Draw the rectangle
        e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black), 2 ), r );

        if ( e.State == ( DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
        {
          // if the item is not selected draw it with a different color
          e.Graphics.FillRectangle(new SolidBrush(Color.White) , r);
          e.Graphics.DrawString( s , fn , new SolidBrush(Color.Black), r ,sf);
          e.DrawFocusRectangle();
        }
        else
        {
          // if the item is selected draw it with a different color
          e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue) , r);
          e.Graphics.DrawString( s , fn , new SolidBrush(Color.Red), r ,sf);
          e.DrawFocusRectangle();
        }
      }
    }

    private void comboBox2_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
    {
      // Override this function to draw items in the Color comboBox

      // Get the Graphics Object (aka. CDC or Device Context Object )
      // passed via the DrawItemEventArgs parameter
      Graphics g = e.Graphics ;

      // Get the bounding rectangle of the item currently being painted
      Rectangle r = e.Bounds ;

      if ( e.Index >= 0 ) 
      {
        Rectangle rd = r ; 
        rd.Width = 100 ; 
        
        Rectangle rt = r ;
        r.Left = rd.Right ; 

        // Get the brush object, at the specifid index in the colorArray
        SolidBrush b = (SolidBrush)colorArray[e.Index];
        // Fill a portion of the rectangle with the selected brush
        g.FillRectangle(b  , rd);

        // Set the string format options
        StringFormat sf = new StringFormat();
        sf.Alignment = StringAlignment.Near;

        Console.WriteLine(e.State.ToString());
        
        // Draw the rectangle
        e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black), 2 ), r );

        if ( e.State == ( DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
        {
          // if the item is not selected draw it with a different color
          e.Graphics.FillRectangle(new SolidBrush(Color.White) , r);
          e.Graphics.DrawString( b.Color.Name, new Font("Ariel" , 8 , FontStyle.Bold  ) , new SolidBrush(Color.Black), r ,sf);
          e.DrawFocusRectangle();
        }
        else
        {
          // if the item is selected draw it with a different color
          e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue) , r);
          e.Graphics.DrawString( b.Color.Name, new Font("Veranda" , 12 , FontStyle.Bold  ) , new SolidBrush(Color.Red), r ,sf);
          e.DrawFocusRectangle();
        }
      }
    }

    private void MyItemSelected(object sender, System.EventArgs e)
    {
      // UnBox the sender. Since both the ComboBox controls use the same
      // events to handle the selection of item from the list

      ComboBox cb = null ;
      if ( sender.Equals(comboBox1))
        cb = comboBox1; 
      else
        cb = comboBox2; 
      
      // Get the index of the Item Selected
            int x = cb.SelectedIndex ;

      string str = "";
      // Get the Text of the item selected
      if ( sender.Equals(comboBox1))
      {
        str = "Item Selected is = " + (string)cb.Items[x];
      }
      else
      {
        SolidBrush br = (SolidBrush)colorArray[x];
        str = "Color Selected is = " + br.Color.Name;
      }
      
      MessageBox.Show(str,"ComboBox Item");
    }

    private void comboBox1_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
    {
      // For the comboBox with OwnerDrawVariable property
      // Display every second item with a height of 45
      if ( e.Index%2 == 0  )
      {
        e.ItemHeight = 45 ;
        e.ItemWidth = 20 ; 
      }
      else
      {
        // Display all other items with a height of 25
        e.ItemHeight = 25 ;
        e.ItemWidth = 10 ; 
      }
    }
  }
}


           
          


ComboBox.zip( 65 k)

WindowsXP controls


   

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsXP
{
    /// <summary>
    /// Summary description for WindowsXP.
    /// </summary>
    public class WindowsXP : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.HScrollBar HScrollBar1;
        internal System.Windows.Forms.ProgressBar ProgressBar1;
        internal System.Windows.Forms.TrackBar TrackBar1;
        internal System.Windows.Forms.ComboBox ComboBox1;
        internal System.Windows.Forms.RadioButton RadioButton1;
        internal System.Windows.Forms.CheckBox CheckBox1;
        internal System.Windows.Forms.Button Button1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public WindowsXP()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.HScrollBar1 = new System.Windows.Forms.HScrollBar();
            this.ProgressBar1 = new System.Windows.Forms.ProgressBar();
            this.TrackBar1 = new System.Windows.Forms.TrackBar();
            this.ComboBox1 = new System.Windows.Forms.ComboBox();
            this.RadioButton1 = new System.Windows.Forms.RadioButton();
            this.CheckBox1 = new System.Windows.Forms.CheckBox();
            this.Button1 = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.TrackBar1)).BeginInit();
            this.SuspendLayout();
            // 
            // HScrollBar1
            // 
            this.HScrollBar1.Location = new System.Drawing.Point(24, 256);
            this.HScrollBar1.Name = "HScrollBar1";
            this.HScrollBar1.Size = new System.Drawing.Size(240, 20);
            this.HScrollBar1.TabIndex = 15;
            // 
            // ProgressBar1
            // 
            this.ProgressBar1.Location = new System.Drawing.Point(24, 216);
            this.ProgressBar1.Name = "ProgressBar1";
            this.ProgressBar1.Size = new System.Drawing.Size(128, 23);
            this.ProgressBar1.TabIndex = 14;
            this.ProgressBar1.Value = 30;
            // 
            // TrackBar1
            // 
            this.TrackBar1.Location = new System.Drawing.Point(16, 152);
            this.TrackBar1.Name = "TrackBar1";
            this.TrackBar1.Size = new System.Drawing.Size(164, 45);
            this.TrackBar1.TabIndex = 13;
            // 
            // ComboBox1
            // 
            this.ComboBox1.Location = new System.Drawing.Point(24, 116);
            this.ComboBox1.Name = "ComboBox1";
            this.ComboBox1.Size = new System.Drawing.Size(160, 21);
            this.ComboBox1.TabIndex = 12;
            this.ComboBox1.Text = "ComboBox1";
            // 
            // RadioButton1
            // 
            this.RadioButton1.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.RadioButton1.Location = new System.Drawing.Point(24, 88);
            this.RadioButton1.Name = "RadioButton1";
            this.RadioButton1.Size = new System.Drawing.Size(172, 16);
            this.RadioButton1.TabIndex = 11;
            this.RadioButton1.Text = "RadioButton1";
            // 
            // CheckBox1
            // 
            this.CheckBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.CheckBox1.Location = new System.Drawing.Point(24, 56);
            this.CheckBox1.Name = "CheckBox1";
            this.CheckBox1.Size = new System.Drawing.Size(160, 20);
            this.CheckBox1.TabIndex = 10;
            this.CheckBox1.Text = "CheckBox1";
            // 
            // Button1
            // 
            this.Button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.Button1.Location = new System.Drawing.Point(24, 12);
            this.Button1.Name = "Button1";
            this.Button1.Size = new System.Drawing.Size(104, 28);
            this.Button1.TabIndex = 9;
            this.Button1.Text = "Button1";
            // 
            // WindowsXP
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 298);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.HScrollBar1,
                                                                          this.ProgressBar1,
                                                                          this.TrackBar1,
                                                                          this.ComboBox1,
                                                                          this.RadioButton1,
                                                                          this.CheckBox1,
                                                                          this.Button1});
            this.Name = "WindowsXP";
            this.Text = "Windows XP Controls";
            ((System.ComponentModel.ISupportInitialize)(this.TrackBar1)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new WindowsXP());
        }
    }
}


           
          


Add control to a window


   

using System;
using System.Windows.Forms;

public class MyForm : Form{

  void btn1_onclick(object sender, EventArgs e)
  {
    Text = "Sender: " + sender.ToString() + " - Event: " + e.ToString();
  }

  void btn1_onclick2(object sender, EventArgs e){
    Console.WriteLine(String.Format("Sender: {0} - Event: {1}", sender.ToString(), e.ToString()));
  }

  public MyForm() {
    Text = "Hello World";

    Button btn1 = new Button();
    btn1.Text = "Click Me";
    this.Controls.Add(btn1);

    btn1.Click += new EventHandler(btn1_onclick);
    btn1.Click += new EventHandler(btn1_onclick2);
  }

  public static void Main()
  {
    Application.Run(new MyForm());
  }

}


           
          


Use ContentAlignment

   
 
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

class MainForm : System.Windows.Forms.Form {
    ContentAlignment currAlignment = ContentAlignment.MiddleCenter;
    int currEnumPos = 0;

    public MainForm() {
        InitializeComponent();
        CenterToScreen();
    }

    protected void btnStandard_Click(object sender, System.EventArgs e) {
        Array values = Enum.GetValues(currAlignment.GetType());

        currEnumPos++;
        if (currEnumPos >= values.Length)
            currEnumPos = 0;

        currAlignment = (ContentAlignment)Enum.Parse(currAlignment.GetType(),
                        values.GetValue(currEnumPos).ToString());
        btnStandard.TextAlign = currAlignment;

        btnStandard.Text = currAlignment.ToString();

        btnImage.ImageAlign = currAlignment;
    }
    private void InitializeComponent() {
        this.btnStandard = new System.Windows.Forms.Button();
        this.btnFlat = new System.Windows.Forms.Button();
        this.btnImage = new System.Windows.Forms.Button();
        this.btnPopup = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // btnStandard
        // 
        this.btnStandard.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
        this.btnStandard.ForeColor = System.Drawing.SystemColors.ControlText;
        this.btnStandard.Location = new System.Drawing.Point(16, 80);
        this.btnStandard.Name = "btnStandard";
        this.btnStandard.Size = new System.Drawing.Size(312, 88);
        this.btnStandard.TabIndex = 2;
        this.btnStandard.Text = "I am a standard button";
        this.btnStandard.Click += new System.EventHandler(this.btnStandard_Click);
        // 
        // btnFlat
        // 
        this.btnFlat.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        this.btnFlat.ForeColor = System.Drawing.Color.Blue;
        this.btnFlat.Location = new System.Drawing.Point(16, 24);
        this.btnFlat.Name = "btnFlat";
        this.btnFlat.Size = new System.Drawing.Size(152, 32);
        this.btnFlat.TabIndex = 0;
        this.btnFlat.Text = "I am flat...";
        // 
        // btnImage
        // 
        this.btnImage.Font = new System.Drawing.Font("Microsoft Sans Serif", 20F, System.Drawing.FontStyle.Bold);
        this.btnImage.ForeColor = System.Drawing.Color.Black;
        this.btnImage.Location = new System.Drawing.Point(16, 192);
        this.btnImage.Name = "btnImage";
        this.btnImage.Size = new System.Drawing.Size(312, 72);
        this.btnImage.TabIndex = 3;
        this.btnImage.Text = "Image Button";
        this.btnImage.TextAlign = System.Drawing.ContentAlignment.TopCenter;
        // 
        // btnPopup
        // 
        this.btnPopup.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
        this.btnPopup.ForeColor = System.Drawing.SystemColors.ControlText;
        this.btnPopup.Location = new System.Drawing.Point(176, 24);
        this.btnPopup.Name = "btnPopup";
        this.btnPopup.Size = new System.Drawing.Size(152, 32);
        this.btnPopup.TabIndex = 1;
        this.btnPopup.Text = "I am a Popup!";
        // 
        // MainForm
        // 
        this.ClientSize = new System.Drawing.Size(340, 269);
        this.Controls.Add(this.btnImage);
        this.Controls.Add(this.btnStandard);
        this.Controls.Add(this.btnPopup);
        this.Controls.Add(this.btnFlat);
        this.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
        this.Name = "MainForm";
        this.Text = "Buttons";
        this.ResumeLayout(false);

    }


    private System.ComponentModel.Container components = null;
    private System.Windows.Forms.Button btnImage;
    private System.Windows.Forms.Button btnStandard;
    private System.Windows.Forms.Button btnPopup;
    private System.Windows.Forms.Button btnFlat;

    [STAThread]
    public static void Main(string[] args) {
        Application.Run(new MainForm());
    }
}

    


Form Anchor


   

/*
C# Programming Tips &amp; Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Anchor
{
  /// <summary>
  /// Summary description for Form1.
  /// </summary>
  public class FormAnchor : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Button btnOK;
    private System.Windows.Forms.Button btnCancel;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Button btnClear;
    private System.Windows.Forms.CheckBox checkBox1;
    private System.Windows.Forms.CheckBox checkBox2;
    private System.Windows.Forms.CheckBox checkBox3;
    private System.Windows.Forms.RadioButton radioButton1;
    private System.Windows.Forms.RadioButton radioButton2;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    public FormAnchor()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

      //
      // TODO: Add any constructor code after InitializeComponent call
      //
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null) 
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.btnOK = new System.Windows.Forms.Button();
      this.btnCancel = new System.Windows.Forms.Button();
      this.textBox1 = new System.Windows.Forms.TextBox();
      this.btnClear = new System.Windows.Forms.Button();
      this.checkBox1 = new System.Windows.Forms.CheckBox();
      this.checkBox2 = new System.Windows.Forms.CheckBox();
      this.checkBox3 = new System.Windows.Forms.CheckBox();
      this.radioButton1 = new System.Windows.Forms.RadioButton();
      this.radioButton2 = new System.Windows.Forms.RadioButton();
      this.SuspendLayout();
      // 
      // btnOK
      // 
      this.btnOK.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
      this.btnOK.Location = new System.Drawing.Point(106, 188);
      this.btnOK.Name = "btnOK";
      this.btnOK.Size = new System.Drawing.Size(72, 32);
      this.btnOK.TabIndex = 0;
      this.btnOK.Text = "Bottom";
      // 
      // btnCancel
      // 
      this.btnCancel.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
      this.btnCancel.Location = new System.Drawing.Point(196, 188);
      this.btnCancel.Name = "btnCancel";
      this.btnCancel.Size = new System.Drawing.Size(72, 32);
      this.btnCancel.TabIndex = 1;
      this.btnCancel.Text = "Bottom Right";
      // 
      // textBox1
      // 
      this.textBox1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
        | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right);
      this.textBox1.Location = new System.Drawing.Point(72, 80);
      this.textBox1.Multiline = true;
      this.textBox1.Name = "textBox1";
      this.textBox1.Size = new System.Drawing.Size(140, 60);
      this.textBox1.TabIndex = 2;
      this.textBox1.Text = "Top Bottom Left Right";
      // 
      // btnClear
      // 
      this.btnClear.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
      this.btnClear.Location = new System.Drawing.Point(16, 188);
      this.btnClear.Name = "btnClear";
      this.btnClear.Size = new System.Drawing.Size(72, 32);
      this.btnClear.TabIndex = 3;
      this.btnClear.Text = "Bottom Left";
      // 
      // checkBox1
      // 
      this.checkBox1.Location = new System.Drawing.Point(16, 8);
      this.checkBox1.Name = "checkBox1";
      this.checkBox1.Size = new System.Drawing.Size(56, 24);
      this.checkBox1.TabIndex = 4;
      this.checkBox1.Text = "Top Left";
      // 
      // checkBox2
      // 
      this.checkBox2.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
      this.checkBox2.Location = new System.Drawing.Point(228, 8);
      this.checkBox2.Name = "checkBox2";
      this.checkBox2.Size = new System.Drawing.Size(64, 24);
      this.checkBox2.TabIndex = 5;
      this.checkBox2.Text = "Top Right";
      // 
      // checkBox3
      // 
      this.checkBox3.Anchor = System.Windows.Forms.AnchorStyles.Top;
      this.checkBox3.Location = new System.Drawing.Point(106, 8);
      this.checkBox3.Name = "checkBox3";
      this.checkBox3.Size = new System.Drawing.Size(80, 24);
      this.checkBox3.TabIndex = 6;
      this.checkBox3.Text = "Top";
      // 
      // radioButton1
      // 
      this.radioButton1.Anchor = System.Windows.Forms.AnchorStyles.Left;
      this.radioButton1.Location = new System.Drawing.Point(8, 82);
      this.radioButton1.Name = "radioButton1";
      this.radioButton1.Size = new System.Drawing.Size(48, 48);
      this.radioButton1.TabIndex = 7;
      this.radioButton1.Text = "Left";
      // 
      // radioButton2
      // 
      this.radioButton2.Anchor = System.Windows.Forms.AnchorStyles.Right;
      this.radioButton2.Location = new System.Drawing.Point(228, 82);
      this.radioButton2.Name = "radioButton2";
      this.radioButton2.Size = new System.Drawing.Size(56, 48);
      this.radioButton2.TabIndex = 8;
      this.radioButton2.Text = "Right";
      // 
      // FormAnchor
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(296, 245);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.radioButton2,
                                      this.radioButton1,
                                      this.checkBox3,
                                      this.checkBox2,
                                      this.checkBox1,
                                      this.btnClear,
                                      this.textBox1,
                                      this.btnCancel,
                                      this.btnOK});
      this.Name = "FormAnchor";
      this.Text = "FormAnchor";
      this.ResumeLayout(false);

    }
    #endregion

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
      Application.Run(new FormAnchor());
    }
  }
}


           
          


Use Control.GetType to check the control type

using System;
using System.Drawing;
using System.Windows.Forms;

class CustomCheckBox: Form
{
public static void Main()
{
Application.Run(new CustomCheckBox());
}
public CustomCheckBox()
{
int cyText = Font.Height;
int cxText = cyText / 2;
FontStyle[] afs = { FontStyle.Bold, FontStyle.Italic,
FontStyle.Underline, FontStyle.Strikeout };

Label label = new Label();
label.Parent = this;
label.Text = “Sample Text”;

for (int i = 0; i < 4; i++) { FontStyleCheckBox chkbox = new FontStyleCheckBox(); chkbox.Parent = this; chkbox.Text = afs[i].ToString(); chkbox.fontstyle = afs[i]; chkbox.Location = new Point(2 * cxText, (4 + 3 * i) * cyText / 2); chkbox.Size = new Size(12 * cxText, cyText); chkbox.CheckedChanged += new EventHandler(CheckBoxOnCheckedChanged); } } void CheckBoxOnCheckedChanged(object obj, EventArgs ea) { FontStyle fs = 0; Label label = null; for (int i = 0; i < Controls.Count; i++) { Control ctrl = Controls[i]; if (ctrl.GetType() == typeof(Label)) label = (Label) ctrl; else if (ctrl.GetType() == typeof(FontStyleCheckBox)) if (((FontStyleCheckBox) ctrl).Checked) fs |= ((FontStyleCheckBox) ctrl).fontstyle; } label.Font = new Font(label.Font, fs); } } class FontStyleCheckBox: CheckBox { public FontStyle fontstyle; } [/csharp]