简体   繁体   English

C#以第二种形式创建动态文本框

[英]c# creating dynamic textbox in a second form

I am trying to write a code in order to create dynamic textboxes. 我正在尝试编写代码以创建动态文本框。

I have Function class and have a second form in my program named ProductForm.cs 我有Function类,并且在我的程序中有另一个名为ProductForm.cs表单。

What I wanna do is to read some data with a function named GetSpecs in my Function.cs and than inside GetSpecs I want to call a function in another class and send data to my other function under ProductForm.cs class. 我想做的是在Function.cs名为GetSpecs的函数读取一些数据,而不是在GetSpecs内部读取数据,我想在另一个类中调用一个函数并将数据发送到ProductForm.cs类下的另一个函数。

I am getting blank form at the end. 我最后得到的是空白表格。

a part of my GetSpecs function: 我的GetSpecs函数的一部分:

private String GetSpecs(String webData)
{
   ......
   ProductForm form2 = new ProductForm();
   form2.CreateTextBox(n);
}

ProductForm.cs ProductForm.cs

public void CreateTextBox(int i)
    {
        ProductForm form2 = new ProductForm();
        form2.Visible = true;
        form2.Activate();

        int x = 10;
        int y = 10;
        int width = 100;
        int height = 20;

        for (int n = 0; n < i; n++)
        {
            for (int row = 0; row < 4; row++)
            {
                String name = "txtBox_" + row.ToString();
                TextBox tb = new TextBox();
                tb.Name = name;

                tb.Location = new Point(x, y);
                tb.Height = height;
                tb.Width = width + row * 2;
                x += 25 + row * 2;
                this.Controls.Add(tb);

            }
            y += 25;

        }

    }

I get a blank form of ProductForm. 我得到一个空白表格的ProductForm。 Textboxes are not created or I cannot see them. 文本框未创建或我看不到它们。

If I put textbox inside 如果我在里面放文本框

private void ProductForm_Load(object sender, EventArgs e)

I can see textboxes. 我可以看到文本框。

你创造出一个全新的ProductForm (实例中的form2变量),然后将控件添加到this (这是从来没有显示)。

You are adding the controls to the current form: this.Controls.Add(tb); 您正在将控件添加到当前表单: this.Controls.Add(tb); , you need to add them to the other form: ,您需要将它们添加到其他形式:

form2.Controls.Add(tb);

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

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