【问题标题】:For Loop will not Create New Textboxes in a C# Windows Forms ApplicationFor 循环不会在 C# Windows 窗体应用程序中创建新文本框
【发布时间】:2021-09-23 00:24:46
【问题描述】:

我打算让它创建与 numericUpDown1 的值一样多的文本框,我将进行一些调整,以便它在值降低时也删除文本框,但它根本不起作用。我不太确定为什么会这样。

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;
      }
}

【问题讨论】:

  • 可能是因为您没有将新的 TextBox 控件添加到任何容器中,所以它们处于 limbo 中。 -- 我建议使用 FlowLayoutPanel 作为容器和支持集合,作为Dictionary&lt;int, TextBox&gt;。 -- 请注意,要删除控件,您必须将其处理掉。要添加它,您应该将一个或多个添加到集合中,但不要在每次增加计数器时重复相同的循环(因此,您可能应该存储以前的值并使用差异)。

标签: c# winforms for-loop textbox numericupdown


【解决方案1】:

您需要将文本框控件添加到表单中才能显示出来。

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();
}

【讨论】:

    【解决方案2】:

    你可以这样做:

    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);
        }
    }
    

    但是在你正在做的那个循环中,你将添加比 Numeric UpDown 中指定的更多的文本框,如果你第一次单击它,它将是 1,所以代码将添加一个文本框,下一次它会将 2 加到现有的上,当 Numeric UpDwon 为 2 时将它们变为 3,依此类推...

    为了在数字 updown 值减少时删除它们,您需要从 Form.Controls 属性中引用这些文本框。

    【讨论】:

      【解决方案3】:

      试试这个:

      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);
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2020-01-02
        • 1970-01-01
        • 1970-01-01
        • 2011-08-24
        • 1970-01-01
        • 2014-12-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多