Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to access a control (ToolStripStatusLabel) of a MDI parent form from a child form. Can anyone please suggest me how to do access it?

Thanks in advance.
Posted
Updated 2-Jan-13 20:32pm
v3

First you need to make public to ToolStripStatusLabel then try this:
C#
Form1 parent = (Form1)this.MdiParent;
            var v = parent.ToolStripStatusLabel ;
 
Share this answer
 
Comments
sahabiswarup 3-Jan-13 2:54am    
getting Null Reference Exception.
Object reference not set to an instance of an object.
hemantkulshrestha 3-Jan-13 3:58am    
Make sure MDIParent property should not be null. Here "this" should be mdi child and its MDIParent property should have reference of MDI form.
Let me know if you need any help.
sahabiswarup 3-Jan-13 4:10am    
can you please explain you are talking about what type MDIParent property?
hemantkulshrestha 3-Jan-13 4:41am    
If you want to make a form MDI Parent you need to set IsMDIContaier property true. Now if you want another form to be open as MDI child then you need to set its MdiParent property with above form object. like

Form2 f = new Form2();
f.MdiParent = this; ;// here "this" is a MDI container means MDI Parent
f.Show();
sahabiswarup 3-Jan-13 6:33am    
Form2 f = new Form2();
f.MdiParent = this; ;// here "this" is a MDI container means MDI Parent f.Show();
so i have to add this above code into MDIParent initialize ?
Hello,
The best answer if you were accessing the controls inside a Mdiparent from a child form, see this:

Put this code in your mdichild form:

Steps:
1. Cast the StatusStrip type into your control
2. Cast the MDI parent form.
3. Assuming your statusstrip control name is statusStrip1:

C++
foreach (ToolStripItem item in ((StatusStrip)((Form)this.ParentForm).Controls["statusStrip1"]).Items)
    {
        if (item.Name =="toolStripStatusLabel1") // you can loop to controls that reside into it. using another iteration but since i have one toolstriplabel, i don't use the iteration here.
           item.Text = "Welcome";
    }


Regards,
Kix46
 
Share this answer
 
Hi,

MDI form code is below
C#
public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
       }

       private void btnOpen_Click(object sender, EventArgs e)
       {
           Form2 frm = new Form2(toolStripStatusLabel1);
           frm.MdiParent = this;
           frm.Show();
       }
   }

and the child form code is below

C#
public partial class Form2 : Form
   {

       public Form2(ToolStripStatusLabel statusStripLabel)
       {
           InitializeComponent();
           statusStripLabel.BackColor = Color.Black;

       }
   }


Regards,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900