I want to dynamically create a number Buttonby specifying a range in TextBox.
The problem is that when I enter a range, for example (3). It creates 3 buttons, but then when I give the range less than the range indicated before, for example, (2). it does not show me 2 buttons, it shows me the previous 3 buttons. My code runs in a range that exceeds the previous range, but it fails when the new range is smaller than the previous range.
Here is my code:
private void button2_Click(object sender, EventArgs e)
{
int number = int.Parse(textBox3.Text);
Button[] textBoxes = new Button[number];
int location = 136;
for (int i = 0; i < textBoxes.Length; i++)
{
location += 81;
var txt = new Button();
textBoxes[i] = txt;
txt.Name = "text" + i.ToString();
txt.Text = "textBox" + i.ToString();
txt.Location = new Point(location, 124);
txt.Visible = true;
this.Controls.Add(txt);
}
}
Loyal source
share