none
Adding controls to a form dynamically

    Question

  • How to add checkboxes at a particular positon in a application dynamically.

    That is ,I want to add checkboxs depending on the no of items in a collection and i even want to name it.How can i do it?

    Can i get any sample code for it?

    • Moved by CoolDadTxMVP Wednesday, September 29, 2010 1:57 PM Winforms related (From:Visual C# General)
    Wednesday, September 29, 2010 1:50 PM

Answers

  • here's the code

    //let's say ur collection is col
    for (int i = 0; i <col.Length ; i++)
          {
           CheckBox chk = new CheckBox();
          chk.Name = "chk1"+i.ToString();
          chk.Left = 100;
          chk.Top = this.Top + 10*i;
          chk.Text = "check box one";
          this.Controls.Add(chk); 
          }
    


    Regards Kumar Gaurav. Please Mark as an answer in case the post is helpful to you so that it became helpful to others
    • Marked as answer by guest87 Wednesday, September 29, 2010 2:24 PM
    Wednesday, September 29, 2010 1:58 PM

All replies

  • here's the code

    //let's say ur collection is col
    for (int i = 0; i <col.Length ; i++)
          {
           CheckBox chk = new CheckBox();
          chk.Name = "chk1"+i.ToString();
          chk.Left = 100;
          chk.Top = this.Top + 10*i;
          chk.Text = "check box one";
          this.Controls.Add(chk); 
          }
    


    Regards Kumar Gaurav. Please Mark as an answer in case the post is helpful to you so that it became helpful to others
    • Marked as answer by guest87 Wednesday, September 29, 2010 2:24 PM
    Wednesday, September 29, 2010 1:58 PM
  • Hi, guest87. See this example:

     

      public partial class Form1 : Form
      {
        private List<int> items = new List<int>();
        private List<CheckBox> checkBoxes = new List<CheckBox>();
    
        public Form1()
        {
          InitializeComponent();
          items.Add(1);
          items.Add(2);
          items.Add(3);
          items.Add(4);
          items.Add(5);
          items.Add(6);
        }
    
        private void btnAddCheckboxes_Click(object sender, EventArgs e)
        {
          CheckBox chk;
    
          for (int i = 0; i < items.Count; i++)
          {
            chk = new CheckBox();
            chk.Name = "chkItem" + items[i].ToString();
            chk.Text = "Item " + items[i].ToString();
            chk.Location = new Point(10, 10 + (i * 25));
            checkBoxes.Add(chk);
            this.Controls.Add(chk);
          }
        }
    
        private void btnClearCheckBoxes_Click(object sender, EventArgs e)
        {
          foreach (CheckBox chk in checkBoxes)
            this.Controls.Remove(chk);
    
          checkBoxes.Clear();
        }
      }
    

    I hope this helps.

    Regards,

    Fábio


    "To alcohol! The cause of and solution to all of life's problems." - Homer Simpson
    Wednesday, September 29, 2010 2:03 PM
  • Thanks that was what i was looking for..☺
    Wednesday, September 29, 2010 2:25 PM