Sunday 8 May 2011

How to Add Manually CheckBox in C#


You can add a CheckBox to a form at run time in the following manner.
Code for Add mannualy CheckBox in C#:


public void AddCheckBox()
{
  CheckBox chkBox = new CheckBox();
  chkBox.Location = new Point(50, 50);
 
  chkBox.AutoCheck = true;
  chkBox.Text = "Checked";
  chkBox.Checked = true;
  chkBox.CheckState = CheckState.Checked;
 
  chkBox.CheckedChanged += new EventHandler(chkBox_CheckedChanged);
 
  Controls.Add(chkBox);
}
 
private void chkBox_CheckedChanged(object sender, System.EventArgs e)
{
  if (sender is CheckBox)
  {
    CheckBox checkbox = sender as CheckBox;
    if (checkbox.Checked)
    {
      checkbox.Text = "Checked";
    }
    else
    {
      checkbox.Text = "UnChecked";
    }
  }
}

No comments:

Post a Comment