开发者

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

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.

I am getting blank form at the end.

a part of my GetSpecs function:

private String GetSpecs(String webData)开发者_开发技巧
{
   ......
   ProductForm form2 = new ProductForm();
   form2.CreateTextBox(n);
}

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


You're creating showing a brand new ProductForm instance (in the form2 variable), then adding controls to this (which is never shown).


You are adding the controls to the current form: this.Controls.Add(tb);, you need to add them to the other form:

form2.Controls.Add(tb);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜