c# - How do I re-access the usercontrol that i've been declared before in a child control? -


i want reaccess of child usercontrol main form..i want access object "watch" i've declared watchlistuc watch = new watchlistuc();

from main i've declared user control on panel of main form

 private void mylist_load(object sender, eventargs e)     {         loginscreen screen = new loginscreen();         panel2.controls.clear();         panel2.controls.add(screen);         loaddb();         grid.contextmenustrip = opendetails;      } 


after created login , there able call watchlistuc watch = new watchlistuc(); want recall later

on login screen here's code

private void login_click(object sender, eventargs e)     {         suspendlayout();         try         {             mysqlconnection conn = new mysqlconnection(myconnection);             conn.open();             mysqlcommand command = new mysqlcommand("select * maindatabase.users user=?parameter1 , pass=?parameter2;", conn);             command.parameters.addwithvalue("?parameter1", user.text);             command.parameters.addwithvalue("?parameter2", pass.text);             mysqldatareader reader = command.executereader();              int ctr = 0;             while (reader.read())             {                 ctr++;                // controlnum = reader["idnum"].tostring();                 mylist.accountcontrolnum = int.parse(reader["idnum"].tostring());                // messagebox.show(mylist.accountcontrolnum.tostring());             }             if (ctr == 1)             {                 this.parent.controls.remove(this);                 mylist = mylist.activeform mylist;                 useraccount acc = new useraccount();                 my.panel2.controls.add(acc);                 my.label1.text = reader["user"].tostring()+" 'list";                 watchlistuc watch = new watchlistuc();                 my.panel3.controls.clear();                 my.panel3.controls.add(watch);                 finishlistuc finish = new finishlistuc();                 my.panel4.controls.clear();                 my.panel4.controls.add(finish);               //  messagebox.show("success!");             }             else             {                 messagebox.show("invalid username or password!");             }              conn.close();             resumelayout();         }         catch (exception ex)         {             messagebox.show("error" + ex);             resumelayout();         }         resumelayout();     } 


now on main form how reaccess here after "insertwl()" method??

void confirmedwl()     {         suspendlayout();         try         {             mysqlconnection conn = new mysqlconnection(myconnection);             conn.open();             mysqlcommand command = new mysqlcommand("select * maindatabase.watchlist controlnum=?cn , idnum=?id;", conn);             command.parameters.addwithvalue("?cn", int.parse(a.tostring()));             command.parameters.addwithvalue("?id", mylist.accountcontrolnum);             mysqldatareader reader = command.executereader();              int ctr = 0;             while (reader.read())             {                 ctr++;              }             if (ctr == 1)             {                 messagebox.show("already existed!");             }             else             {                 insertwl();                                     //watchlistuc watch1 = panel3.controls.find("watch", true).defaultifempty() watchlistuc;                 //watch1.dvgrefresh();                //here want recall watch can call method dvgrefresh();             }               conn.close();             resumelayout();         }         catch { }     }<br> 

i hoping me here's full code , screen shots http://www.mediafire.com/download/1l18e6v8158mi16/help_please.rar

usercontrol visual objects, , can used every object.

that means can store references in variable within scope fits best you.

in example, define watch @ form level:

watchlistuc watch; 

then, in login_click event method, there nothing wrong doing this:

watch = new watchlistuc(); my.panel3.controls.clear(); my.panel3.controls.add(watch); 

finally, in confirmedwl method, use watch instance have.

... else {     insertwl();                         watch.dvgrefresh(); } 

the drawback of approach have careful control lifecycle:

  • watch null if not instantiated, , can lead nullreferenceexception if try use without putting new control inside it.
  • also, remember call .dispose() on controls created on fly when finished them. not required if plan have 1 instance of watchlistuc during application life cycle.

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -