1

I want to reaccess some of my child usercontrol from my main form..I want to access the object "watch" that i've declared from WatchListUC watch = new WatchListUC();

from my main I've declared this user control on a panel of the 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;

    }


then after that i created a login and there i was able to call the WatchListUC watch = new WatchListUC(); which i want to recall later

on the login screen here's the code

private void LogIn_Click(object sender, EventArgs e)
    {
        SuspendLayout();
        try
        {
            MySqlConnection conn = new MySqlConnection(myConnection);
            conn.Open();
            MySqlCommand command = new MySqlCommand("SELECT * FROM maindatabase.users where user=?parameter1 and 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 my = MyList.ActiveForm as 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 my main form how I do I reaccess here after the "insertWL()" method??

void ConfirmedWL()
    {
        SuspendLayout();
        try
        {
            MySqlConnection conn = new MySqlConnection(myConnection);
            conn.Open();
            MySqlCommand command = new MySqlCommand("SELECT * FROM maindatabase.watchlist where ControlNum=?CN and 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() as WatchListUC;
                //watch1.dvgRefresh();

              //here i want to recall the watch so i can call the method dvgRefresh();
            }


            conn.Close();
            ResumeLayout();
        }
        catch { }
    }<br>

I was hoping someone could help me here's my full code and screen shots http://www.mediafire.com/download/1l18e6v8158mi16/Help_please.rar

2 Answers2

1

So if I've understood you correctly, you create your WatchListUC (UserControl) inside of a panel that is on your main form? What I'm confused about is where is this login code located? Is that also inside of your main form?

Main Form
   | - Panel
        | - WatchListUC

As long as you're creating WatchListUC from code inside of your main form, all you need to do is save it to a variable that is outside the scope of your method.

caesay
  • 16,932
  • 15
  • 95
  • 160
  • login is located in a different panel so I have 2 panels panel for login and panel for watchlist each panels create their own user control loginscreen and watchlist so what do you mean i have to create a var outside? and how do i do that sorry for being noob – Raymart Calinao AsthreA Apr 23 '15 at 08:31
  • here's my full codes and screen if you can help me with this one http://www.mediafire.com/download/1l18e6v8158mi16/Help_please.rar – Raymart Calinao AsthreA Apr 23 '15 at 08:53
1

UserControl are visual objects, and can be used like every object.

That means that you can store their references in any variable within the scope that fits the best for you.

In your example, I would define watch at the Form level:

WatchListUC watch;

Then, in the LogIn_Click event method, there is nothing wrong doing this:

watch = new WatchListUC();
my.panel3.Controls.Clear();
my.panel3.Controls.Add(watch);

Finally, in the ConfirmedWL method, just use the watch instance you have.

...
else
{
    insertWL();                    
    watch.dvgRefresh();
}

The drawback of this approach is that you have to be careful about your control lifecycle:

  • watch will be null if it is not instantiated, and this can lead to NullReferenceException if you try to use it without putting a new control inside it.
  • Also, remember to call .Dispose() on the controls you created on the fly when you are finished with them. This is not required if you plan to have only one instance of WatchListUC during your application life cycle.
Larry
  • 17,605
  • 9
  • 77
  • 106
  • ahmm so is my approcah in clearing an usercontrol or disposing is wrong? i mean I use panel.control.clear so my another question is do i really need to use the .dispose() and what are the advantages of using this one? thank you for answering my question.. – Raymart Calinao AsthreA Apr 23 '15 at 12:21
  • Your welcome ! Your approach is correct :) Clearing your container just remove the control from the container, but your watch control still exists and can be reused. Dispose() will prevent this by clearing the internal resources it uses. It is only useful to use Dispose if your code repeatedly creates and remove controls. Otherwise, if you have only one watch instance during your application lifetime, it's not necessary. This other question explains this very well : http://stackoverflow.com/questions/1969705/clear-controls-does-not-dispose-them-what-is-the-risk - Good luck ! – Larry Apr 23 '15 at 12:47
  • thank you very much I've learned a lot from you. i been looking for this solution for month's ahah but you save me there..thanks – Raymart Calinao AsthreA Apr 23 '15 at 13:30