Click here to Skip to main content
15,885,000 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
Hi programmers,

I have two Windows forms: form1 and form2.
When closing form2, I want method form_load in form1 to start. How can I do this????
Posted
Updated 25-Jun-11 15:53pm
v5

I think this is what you want:

in from1 write this:

C#
public void Form1_Load(object sender, EventArgs e)
        {
            MessageBox.Show("hi");
        }


and in from2 write this:

C#
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
    Form1 frm = new Form1();
    frm.Form1_Load(sender,e);

}


hope it helps :)
 
Share this answer
 
Comments
esmailian 26-Jun-11 9:03am    
Eroro:Form1_load(object,System.eventArgs) is Inaccessible due to its protection level
esmailian 26-Jun-11 9:06am    
Yes,it worked if public method in form1 THANKS friend
esmailian 26-Jun-11 9:07am    
I 5 good call
Uday P.Singh 26-Jun-11 9:51am    
thanks :)
You can try this:

In the form_closed event of form2, write:

Form1 frm1 = new Form1(); 
frm1.ShowDialog(); 


Hope this helps. :)
 
Share this answer
 
v3
Comments
esmailian 26-Jun-11 2:18am    
form1 always run.I need method loading restart
Uday P.Singh 26-Jun-11 3:25am    
see my new answer to get what you want.
As the name suggests, this method is the handler of the "fictive" event Load. You should never call it directly!, it will mess-up things. Do you simply need to show another form? Instantiate form class and show instance? What you want to do makes no sense.

If you explain the purpose of it, you might get an advice on what to do.

—SA
 
Share this answer
 
Comments
esmailian 26-Jun-11 2:16am    
I want when closed form2 submit my changes in form1 .
example : back color changes
Sergey Alexandrovich Kryukov 29-Jun-11 5:23am    
Sorry, first this is not the purpose. Why do you want it? Are you saying in form2 the user edits some changes, and they are updated in form1? As the user editing or when the form2 confirmed and closed? Is it modal or not? Why not having a master-detail controls on one form; with detail hidden when not used?
--SA
esmailian 1-Jul-11 10:01am    
Thank , my problem is solved.
Sergey Alexandrovich Kryukov 3-Jul-11 1:54am    
Well, will you accept this answer formally (green button)?
Thanks.
--SA
If you have form1 as your main entry point for the program (i.e. it's always open as long as your program is running), you won't want to reshow the form1 form.

In uday's example above, this assumes you want to create a new instance and how form1.

Instead, I'd recommend making a public method on form1 that form2 calls when form2's formClosing event is triggered.

Put what you want to run in the public method.

Does this "jive" with your thinking?
 
Share this answer
 
v2
Comments
esmailian 26-Jun-11 2:13am    
No, I need change something in form1.loading form1 for submit my changes
jchoponis 26-Jun-11 21:53pm    
I see... I would still argue you could just expose a public property in form1 that would hold your result from form2. What exactly are you passing back to form1? Can you provide a snippet of your project so I can comment?

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