none
C#.net code for create Multiple checkBoxes dynamically

    Question

  • Hi 

    Please give me response quickly,I want code for create Multiple checkBoxes dynamically.Actually i am using this code:-

     CheckBox[] chk;

      chk = new CheckBox[10];

      for (int i = 0; i <= 9; i++)

      {

          chk[i] = new CheckBox();

          chk[i].Name = i.ToString();

          chk[i].Text = i.ToString();

          chk[i].TabIndex = i;

         chk[i].AutoCheck=true;

          panel1.Controls.Add(chk[i]);

     

      }

    But it shows the one checkBox.....there is one problem, All checkboxes are Overlaping. Please anyone rectify my problem.

    Monday, December 06, 2010 11:04 AM

Answers

  • Hello Srinivaas,

    I have modified your code as you share in this thread.

    see below code snippet for your problem's solution.

     

    CheckBox[] chk = new CheckBox[10];
          int height = 1;
          int padding = 10;
    
          for (int i = 0; i <= 9; i++)
          {
    
            chk[i] = new CheckBox();
    
            chk[i].Name = i.ToString();
    
            chk[i].Text = i.ToString();
    
            chk[i].TabIndex = i;
    
            chk[i].AutoCheck = true;
    
            chk[i].Bounds = new Rectangle(10, 20 + padding + height, 40, 22);
    
            panel1.Controls.Add(chk[i]);
    
            height += 22;
    
          }
    
    Hope it helps.


    - Pavan Pareta If you find my post is helpful for you then mark as a answer.
    • Marked as answer by Bin-ze Zhao Thursday, December 09, 2010 6:41 AM
    Tuesday, December 07, 2010 8:48 AM
  • Hi 

    Thanks for response,

    I am using this "chk[i].Location = new System.Drawing.Point(i,i);" But not use.same problem is came.

    There is no available CheckedBoxList also.

    Please give me sample source code please.

    • Marked as answer by Bin-ze Zhao Thursday, December 09, 2010 6:41 AM
    Monday, December 06, 2010 1:08 PM

All replies

  • Hi,

    You need to mention the location also but need to make it generic.

    Regards,

    Malleswar

    Monday, December 06, 2010 12:47 PM
  • Hi 

    Thanks for response,

    I am using this "chk[i].Location = new System.Drawing.Point(i,i);" But not use.same problem is came.

    There is no available CheckedBoxList also.

    Please give me sample source code please.

    • Marked as answer by Bin-ze Zhao Thursday, December 09, 2010 6:41 AM
    Monday, December 06, 2010 1:08 PM
  • Hi,

    Try something like this:

    "chk[i].Location = new System.Drawing.Point(10,i*25);"

     

    Regards,

    Malleswar

     

    Tuesday, December 07, 2010 7:28 AM
  • Hello Srinivaas,

    I have modified your code as you share in this thread.

    see below code snippet for your problem's solution.

     

    CheckBox[] chk = new CheckBox[10];
          int height = 1;
          int padding = 10;
    
          for (int i = 0; i <= 9; i++)
          {
    
            chk[i] = new CheckBox();
    
            chk[i].Name = i.ToString();
    
            chk[i].Text = i.ToString();
    
            chk[i].TabIndex = i;
    
            chk[i].AutoCheck = true;
    
            chk[i].Bounds = new Rectangle(10, 20 + padding + height, 40, 22);
    
            panel1.Controls.Add(chk[i]);
    
            height += 22;
    
          }
    
    Hope it helps.


    - Pavan Pareta If you find my post is helpful for you then mark as a answer.
    • Marked as answer by Bin-ze Zhao Thursday, December 09, 2010 6:41 AM
    Tuesday, December 07, 2010 8:48 AM
  • Hi,

    Thanks for ur suggestions, i solved my problem with ur ideas.... i am using this code ...just changes in Location,like this..

     

    for (int i = 0; i < count; i++)

                {

                    chk[i] = new CheckBox();

                    chk[i].Name = i.ToString();

                    chk[i].Text = wid1[i].ToString();

                    chk[i].Location = new Point(0, i * 20);

     

                    this.Controls.Add(chk[i]);

                }

    this is my successful code.

     

    Thanks to All.

    Saturday, December 11, 2010 6:20 AM
  • Hi... 

    I also want to add a checkbox on the event of a button every time.

    i added the 1 checkbox but when next time i click on that button nothing happened.

    but the index value[i] increases.

    pls send me the code.

    Regards,

    Dheeraj gambhir

    dgambhir143@gmail.com

    Monday, January 06, 2014 6:45 AM
  • hi..

    i have successfully run the cde...thankyou so much...

    Monday, January 06, 2014 7:02 AM
  • Hi..

    with this i successfully added the checkbox but  i want to manage the events on these different checkbox according to their name or id..

    Regards,

    Dheeraj gambhir

    Monday, January 06, 2014 7:19 AM