-3

I was intending to have this create as many textboxes as the value of numericUpDown1, and I'm going to make some tweaks so that it also deletes textboxes when the value is lowered, but it is not functioning at all. I'm not really sure why this is happening.

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
      var numVal = numericUpDown1.Value;

      int count = 2;
      int y = 86;
            
      for (int i = 0; i < numVal; i++)
      {
            //Create the dynamic TextBox.
            TextBox text1 = new TextBox();
            y += 20;
            text1.Location = new Point(719, y);
            text1.Name = "gearText_" + count;
            count += 1;
      }
}
1
  • Probably because you're not adding the new TextBox Controls to any container, so they're in a limbo. -- I suggest to use a FlowLayoutPanel as container and a backing collection, as a Dictionary<int, TextBox>. -- Note that, to remove a Control, you must dispose of it. To add it, you should add one or more to a collection, but not repeat the same loop each time the counter is increased (thus, you should probably store the previous value and use the difference). Commented Jul 14, 2021 at 1:51

3 Answers 3

2

You need to add the textbox control to the form for it to show up.

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
      var numVal = numericUpDown1.Value;

      int count = 2;
      int y = 86;
            
      for (int i = 0; i < numVal; i++)
      {
            //Create the dynamic TextBox.
            TextBox text1 = new TextBox();
            y += 20;
            text1.Location = new Point(719, y);
            text1.Name = "gearText_" + count;
            count += 1;
            Form1.Controls.Add(text1);
      }
      Form1.Show();
}
Sign up to request clarification or add additional context in comments.

Comments

2

You cand do something like this:

public partial class Form1 : Form
{
    private int y = 50;
    public Form1()
    {
        InitializeComponent();
    }

    private void numericUpDown1_ValueChanged(object sender, EventArgs e)
    {
        decimal nudValue = numericUpDown1.Value;

        TextBox tb = new TextBox();

        tb.Location = new Point(50, y += 50);
        tb.Text = "Testbutton";

        this.Controls.Add(tb);
    }
}

But with that cycle you are doing, you are going to add more textboxes than the specified in the Numeric UpDown, if you click it the first time it will be 1, so the code will add one text box, the next time it will add 2 to the existing one, making them 3 when the Numeric UpDwon is 2, and so on ...

In order to delete them when the numeric updown value is decreased you need a reference to those textboxes from the Form.Controls property.

Comments

0

Try this:

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
    {
        var numVal = numericUpDown1.Value;

        int count = 2;
        int y = 86;

        for (int i = 0; i < numVal; i++)
        {
            //Create the dynamic TextBox.
            TextBox text1 = new TextBox();
            y += 20;
            text1.Location = new Point(719, y);
            text1.Name = "gearText_" + count;
            count += 1;

            this.Controls.Add(text1);
        }
    }

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.