forms - How can I properly use methods from a class in another class in C# Visual Studio 2010 -


so, have form2 method public void loadtree() (which loads data database onto treeview in form2), have form3 starts on button click form2. in form3 on button click (after code executes) want use loadtree() method form2.

i have following code in form3 this:

    private void button1_click(object sender, eventargs e)     {         var loading = new form2();         loading.loadtree();         loading.show();     } 

all works , executes well,no errors, @ end of have form2, form3, , form2, if try put code close first form2, entire program shuts down, it?, want after click button on form3, form2 show updated.

i have form2 method public void loadtree() (which loads data database onto treeview in form2

do not make database calls directly in form. put code in class, better yet use n-tier.

in form3 on button click (after code executes) want use loadtree() method form2.

and

i want after click button on form3, form2 show updated.

form3 raises event handled form2. best practice use events communicating messages across forms. use property here, side effect of property , not way accomplish goal.

public partial class form2 : form {              public form2()     {         initializecomponent();     }      private void loadtree(object o, eventargs e)     {         //do work         messagebox.show("loading tree...");     }      private void button1_click(object sender, eventargs e)     {         var form = new form3();         form.loadtreeevent += form_loadtreeevent;//hook form3 event          form.show();     }      void form_loadtreeevent(object o, eventargs e)     {         loadtree(o, e);//handle form3 event     } }  public partial class form3 : form {     //the event raised form3, it's handled form2.     public event loadtreehandler loadtreeevent;             public delegate void loadtreehandler(object o, eventargs e);      public form3()     {         initializecomponent();     }      private void button1_click(object sender, eventargs e)     {         loadtreeevent(null, null);//raise event in form3 , pass whatever         //do work...maybe close form??     } } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -