Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI I am having problem using with delegates. I have multiple windows Forms.from that first from
i place a one button. when i was click on that button .all the forms has to open at a time.
Posted
Comments
OriginalGriff 21-Mar-14 6:03am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.

Are you asking how to create multiple Forms, and show them, when a Button is clicked on a Main Form ? Why use a Delegate instead of just using a simple for-loop, or step-by-step code, in the Button's Click EventHandler ?

When you create new objects dynamically, you probably need to keep track of them, and if you create them in a loop, in a method, you will need keep a valid reference to them.

Consider you have two types of "secondary" Forms, 'Form2, 'Form3. This example:
C#
private List<Form2> theFormTwos = new List<Form2>();

private List<Form3> theFormThrees = new List<Form3>();

private void ButtonOnMainForm_Click(object sender, EventArgs e)
{
    // create two Form2's
    for (int i = 0; i < 2; i++)
    {
        theFormTwos.Add(new Form2());
        // show the Form now ?
    }

    // create three Form3's
    for (int j = 0; j < 3; j++)
    {
        theFormThrees.Add(new Form3());
        // show the Form now ?
    }
}
is storing each created instance of 'Form2 and 'Form3 in a separate List.

Of course, once a Form is shown, then it is accessible in the Application.OpenForms FormCollection, but if you dispose of, or hide (set its 'Visible property to 'false), that Form, it will no longer appear in Application.OpenForms.
 
Share this answer
 
v2
Assuming that the multiple forms don't need to be opened modally (that is, each form would be fighting for focus), why wouldn't you just be able to create an instance of each form in your button click event and call the Show() method on each? You don't mention needing to access each form's properties after they're open so it should be simple:
C#
protected void button1_Click(object sender, EventArgs e)
{
	new Form2().Show();
	new Form3().Show();
	new Form4().Show();
}
 
Share this answer
 
Comments
BillWoodruff 21-Mar-14 9:00am    
Please note that if you create instances of Forms like this, you will have no valid reference outside the Click EventHandler to the instances you create, and if one of those open Forms has its 'Visible property set to 'false, or has its 'Hide command executed, you will have no reference to it, even though it still "exists." See my answer below for one way you can preserve references to Forms created at run-time.
CoderHead 21-Mar-14 9:22am    
100% true, and I said as much in that the question doesn't state that it's necessary to retain a reference to each form or access its properties after opening. It's obviously not optimal but it's at least a very simplistic example of opening multiple forms from a single event. Thanks for your clarification.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900