4

I try to add check box for loop that when i enter 3 for example in textbox and click the button it automatically add 3 check boxes in the form i tried this code but only add one check box

    private void button1_Click(object sender, EventArgs e)
    {                                                         
        int x = Convert.ToInt32(textBox1.Text);
        int m = 1;
        for (int i = 0; i < x; i++)
        {
            CheckBox button2 = new System.Windows.Forms.CheckBox();



            button2.Location = new System.Drawing.Point(5, m);
            button2.Name = "button2 "+  m.ToString();
            button2.Size = new System.Drawing.Size(51, 23);
            button2.TabIndex = m;

            //button2.UseVisualStyleBackColor = true;

            this.Controls.Add(button2);
            m++;



        }
    }           
0

2 Answers 2

5

You are setting the location of all three buttons to nearly the same place so they are displayed on top of each other. Try spacing them out a little more.

For example change m++; to m += 40;.

Sign up to request clarification or add additional context in comments.

1 Comment

And then don't use m for TabIndex. +1
0

You need to space your buttons out a little. Also, you should give each of your buttons a unique ID.

button2.ID = "Button_" + i;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.