user3571238
user3571238

Reputation: 3

c# code to create a new windows form every click of button

I need help on creating a new form when I click a button.

For example, I have a form named create_form then has a button named Create. What I want is to create a form every time I click the Create button, and the forms created will be independent from each other.

I don't want to create a lot of form in Visual Studio like "Form1, Form2, Form3, Form4..etc.." and call each one every time the button is clicked. Then I'd have to be able to create another form in Visual Studio for each time I wanted to click the button, even if I click it 20 times or more. All I want is to create one windows form (Form3 for example) then every time I click the Create button it will just call it and create a new instance of that form. If I click 20 times then there should be 20 forms created and independent from each other.

Upvotes: 0

Views: 1591

Answers (1)

Joel Coehoorn
Joel Coehoorn

Reputation: 416091

Assuming you already have a form named Form3, here is what your button click event will look like:

//use this so you have an easy reference to each of your forms
private myForms As new List<Form3>();

protected void Create_Click(object sender, EventArgs e)
{
     var form = new Form3();
     myForms.Add(form);
     form.Show();
}

Upvotes: 1

Related Questions