简体   繁体   English

用户定义的C#中动态创建的控件的数量

[英]User-defined amount of dynamically created controls in C#

My program is currently set up to just add one row of controls below the original. 我的程序当前设置为仅在原始控件下方添加一行控件。 How can I make it so the user can add as many lines of controls as they would like? 我该如何做,以便用户可以添加任意数量的控件?

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

This is the form with just the one surface added. 这是仅添加一个表面的形式。 I want to make it so there is a new row underneath each time the user clicks +Surface. 我要这样做,以便每次用户单击+ Surface时下面都有一个新行。

具有一个表面的表单

for (int i = 1; i < 2; i++)

在in循环中插入2,替换为n,其中n是从某个控件收集的数字

First step is to take your existing code and create a method that creates a row. 第一步是获取现有代码并创建一个创建行的方法。 Ideally as part of this step, you create a custom control that encapsulates much of the logic you already have now. 理想情况下,作为此步骤的一部分,您将创建一个自定义控件,该控件封装了您现在已经拥有的许多逻辑。

Second step is to take that new method and require a parameter numberOfRows , use that parameter, and default it to 1. Then make sure it still works. 第二步是采用该新方法,并需要一个参数numberOfRows ,使用该参数,并将其默认设置为1。然后确保它仍然有效。

Final step is to provide a method for the user to input numberOfRows and send that to your method. 最后一步是为用户提供一种输入numberOfRows并将其发送给您的方法的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM