0

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.

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

+1 - you need the other instance of the form. BTW, it's not good design to have UI components poke at other UI components. It creates coupling. @Val: read up on MVC.
Many thanks for all of you. You explained it quite well. It is my bad not to see this error. You are all super heroes of mine guys.
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.