【问题标题】:User-defined amount of dynamically created controls in C#用户定义的 C# 中动态创建的控件数量
【发布时间】:2013-01-28 22:48:19
【问题描述】:

我的程序当前设置为仅在原始控件下方添加一行控件。我怎样才能做到这一点,以便用户可以添加任意多的控件行?

private void surfaceAddButton_Click(object sender, EventArgs e)
    {           
        //Adds new set of controls on button click
        for (int i = 1; i < 2; i++)
        {

            ComboBox surfaceCombo2 = new ComboBox();
            TextBox sideWallsText2 = new TextBox();
            TextBox backWallsText2 = new TextBox();
            TextBox floorText2 = new TextBox();
            Label newLabel = new Label();
            Label newLabel2 = new Label();
            Label newLabel3 = new Label();

            Absorption_Coefficients alpha = new Absorption_Coefficients(); //Adds surfaces to combobox
            List<string> materialslist = alpha.listLoad();
            materialslist.Sort();
            surfaceCombo2.Items.AddRange(materialslist.ToArray());
            surfaceCombo2.DropDownStyle = ComboBoxStyle.DropDownList;

            //Sets locations and sizes of new row of options, then displays them
            surfaceCombo2.Location = new Point(1, 150 * i + 30);               
            sideWallsText2.Location = new Point(393, 153 * i + 30);
            backWallsText2.Location = new Point(561, 153 * i + 30);
            floorText2.Location = new Point(711, 153 * i + 30);
            newLabel.Location = new Point(321, 158 * i + 30);
            newLabel2.Location = new Point(439, 158 * i + 30);
            newLabel3.Location = new Point(609, 158 * i + 30);
            surfaceCombo2.Width = 322;
            sideWallsText2.Width = 43;
            backWallsText2.Width = 43;
            floorText2.Width = 43;
            newLabel.Text = "Side Walls, ft²";
            newLabel2.Width = 120;
            newLabel2.Text = "Back/or Front Walls, ft²";
            newLabel3.Text = "Floor/or Ceiling, ft²";

            this.Controls.Add(surfaceCombo2);
            this.Controls.Add(sideWallsText2);
            this.Controls.Add(backWallsText2);
            this.Controls.Add(floorText2);
            this.Controls.Add(newLabel);
            this.Controls.Add(newLabel2);
            this.Controls.Add(newLabel3);

            this.Size = new Size(769, 209 * i + 30); //Increases form to accomodate new controls
        }
    }

这是只添加了一个表面的表格。我想这样做,这样每次用户点击 +Surface 时,下面都会有一个新行。

【问题讨论】:

  • 你试过什么?
  • 考虑在 Gridview 中设置重复自下而上,或者通过滚动支持来处理一对多
  • 我尝试制作一个组合框列表,每次都会添加一个新的组合框,但我不知道如何根据按钮点击的迭代来重命名它添加的新元素。

标签: c# winforms dynamically-generated


【解决方案1】:
for (int i = 1; i < 2; i++)

for 循环中的 2,替换为 n,其中 n 将是从某个控件收集的数字

【讨论】:

    【解决方案2】:

    第一步是获取现有代码并创建一个创建行的方法。理想情况下,作为此步骤的一部分,您可以创建一个自定义控件来封装您现在已经拥有的大部分逻辑。

    第二步是采用该新方法并需要一个参数numberOfRows,使用该参数并将其默认为1。然后确保它仍然有效。

    最后一步是为用户提供输入numberOfRows 的方法并将其发送到您的方法。

    【讨论】:

      猜你喜欢
      • 2014-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多