How to Access MdiParent Controls From Child Forms

Introduction 

When I started programming with C #. Net I have a problem to access some of the controls on parent form through the child form. Example of this control is the menus in the parent window, but after I was used to  programming in C # and spend time with it, I found this method to access the parent window through the child window and thus was able to access all controls will be seen in this article. 

I have here an MdiParent with a menustrip control and want to disable its Menuitems then enable it when I close the child form.



When I click the menu item I disable it for user so as not to open the same form more than one time:

Form2 frm = new Form2();
frm.MdiParent = this;
button1.Enabled = false;
form2ToolStripMenuItem.Enabled = false;
frm.StartPosition = FormStartPosition.CenterScreen;
frm.Show();
		

  

If the control is on the parent form like button1, I can access it by one single line code

//if the control is on the parent Form Direct Like button1 I can access it by one single line code
((Form)this.MdiParent).Controls["button1"].Enabled = true;

But if the control is owned by other control on the parent form, I must access parent form then access the control that hold the Control. I wanna to access like a tree system

Form frm = (Form)this.MdiParent;
MenuStrip ms = (MenuStrip)frm.Controls["menuStrip1"];
ToolStripMenuItem ti = (ToolStripMenuItem)ms.Items["fileToolStripMenuItem"];
ti.DropDownItems["form2ToolStripMenuItem"].Enabled = true;

Now we can customize this code and put it in a class :

class MenuSettings
{
    public static void EnableMenuItem(string MenuName, string MenuItemName)
    {
        //Access the Parent Form
        Form frm = (Form)Application.OpenForms["Form1"];
        //Access The MenuStrip Control
        MenuStrip ms = (MenuStrip)frm.Controls["menuStrip1"];
        //Access The First Level Menu Items
        ToolStripMenuItem ti = (ToolStripMenuItem)ms.Items[MenuName];
        //Then I use This control As my needs
        ti.DropDownItems[MenuItemName].Enabled = true;
    }
    public static void EnableMenuItem(string L1MenuItem, string L2MenuItem, string MenuItemName)
    {
        //Access the Parent Form
        Form frm = (Form)Application.OpenForms["Form1"];
        //Access The MenuStrip Control
        MenuStrip ms = (MenuStrip)frm.Controls["menuStrip1"];
        //Access The First Level Menu Items
        ToolStripMenuItem ti1 = (ToolStripMenuItem)ms.Items[L1MenuItem];
        //Access The Sconed Level Menu Items
        ToolStripMenuItem ti2 = (ToolStripMenuItem)ti1.DropDownItems[L2MenuItem];
        //Then I use This control As my needs
        ti2.DropDownItems[MenuItemName].Enabled = true;
    }
}

And use it in any place I want like form closing event:

MenuSettings.EnableMenuItem("fileToolStripMenuItem", "form3ToolStripMenuItem");

MenuSettings.EnableMenuItem("toolsToolStripMenuItem", "styleToolStripMenuItem", "form6ToolStripMenuItem");

Finally I hope it will useful to you. Have a nice time.

Up Next
    Ebook Download
    View all
    Learn
    View all