简体   繁体   中英

How to access parent form items from child form?

Hello Friends Please help me, I'm new to C# Programming. Kindly help me, I'm unable to integrate my project due to below problem. I have created a MainScreen form, in that I took two panels. First Panel contains project name and a menustrip. In Second panel I'm loading different panels depending on what user click in menustrip. menustrip contain different elements like Home, Update Profile, Search, Book and Logout. By default I'm loading Home form in MainScreen 2nd panel. It kindoff looks like webpage. After logging successfully I want to clear the 2nd panel and want to load Home form/Search form. But when I try to do it, it shows "U cant access panel2 in this context". Please help me, I'm tired of searching solution for it. If this way is not possible, provide me some alternate way. Thanks in Advance!

I used below code...I made mdi parent true too.

private void homeToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Home ob1 = new Home();
        ob1.TopLevel = false;
        ob1.FormBorderStyle = FormBorderStyle.None;
        pnlBody.Controls.Clear();
        pnlBody.Controls.Add(ob1);          
        ob1.Show();
    }

    private void MainScreen_Load(object sender, EventArgs e)
    {
        MainMenuStrip.Items[5].Visible = false;
        Home ob1 = new Home();
        ob1.TopLevel = false;
        ob1.FormBorderStyle = FormBorderStyle.None;
        pnlBody.Controls.Clear();
        pnlBody.Controls.Add(ob1);
        ob1.Show();
    }

You can simply do this by assigning that control Modifier to public. but, it is not a good way to do this. Don't do this. If you want to execute any specific code from the outside the form than you can create a separate method and then create delegate for that method. you can invoke that method by using delegate.Invoke . I have already suggested that in my previous answer .

Let's imagine you have these controls which are from your main page:

button1
textbox1
label1

Now when you click a menu option you need to hide some or all of the controls above and then show these ones:

button2
textbox2
picturebox1
label2

If you want this, then you can just make this in the click event without using panels:

private void homeToolStripMenuItem_Click(object sender, EventArgs e)
{
button1.Visible = false;
textbox1.Visible = false;
label1.Visible = false;

button2.Visible = true;
textbox2.Visible = true;
picturebox1.Visible = true;
label2.Visible = true;

button1.Location = new Point(X, Y);
//Other controls locations...
}

Where the new Point is a class constructor that allows you to change the position of a control in the form (X and Y are pixel coordinates)

And... I guess that's all ~ :3

Ohhh and you could use a public int to count the page number... So if you have for example 3 pages, when the user click, I don't know "Page 2" your variable X which is public will have the value of 2, so in your event you can compare page combinations:

private void homeToolStripMenuItem_Click(object sender, EventArgs e)
{
if(x==1)//You know you are un page 1, you hide all the page 1 controls
{
button1.Visible = false;
textbox1.Visible = false;
label1.Visible = false;
}

else if(x==2)
{
//Hide you page 2 control, etc.
}


//After hidding your controls, next you have to show this page controls and adjust them to the form which are this ones:
button2.Visible = true;
textbox2.Visible = true;
picturebox1.Visible = true;
label2.Visible = true;

button1.Location = new Point(X, Y);
//Other controls locations...


//Finally, set X the value of the page number so you can copy and paste te comparation os X above in your events of every page:
 X = pagenumber;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM