Hello,

I have some app like this:

Form1 (Parent) -> open in load event a Form2 (child) which have a button that open a 3rd one child form (then close Form2).

My question is how I can access a control property (like labeltext (in statusbar) in Form1)?

Form1 Parent

private void Form1_Load(object sender, EventArgs e)
        {
          Form2 chForm2 = new Form2();
          chForm2.MdiParent = this;
          chForm2.Show();                          
         }

Form2 Child

Button click event
            Form3 chForm3 = new Form3();
            chForm3.MdiParent = Form1.ActiveForm;
            chForm3.Show();

Now in Form1 I have a labeltext in statusbar that I want to control from Form3.

Thanks a lot!

Recommended Answers

All 5 Replies

Create a public method in your Form1 to access the control's text, then from the child form, just up-cast your MdiParent to Form1 and access the method:

// defined in Form1, or the MdiParent form...
        public void SetStatusLabelText(string s) 
        {
           toolStripStatusLabel.Text = s;
        }

        // to call from child form:
        MDIParent1 parent = (Form1) this.MdiParent;
        parent.SetStatusLabelText("some text for the status bar...");

Works fine ;) Thanks again!

Create a public method in your Form1 to access the control's text, then from the child form, just up-cast your MdiParent to Form1 and access the method:

// defined in Form1, or the MdiParent form...
        public void SetStatusLabelText(string s) 
        {
           toolStripStatusLabel.Text = s;
        }

        // to call from child form:
        MDIParent1 parent = (Form1) this.MdiParent;
        parent.SetStatusLabelText("some text for the status bar...");

And if I want take a value from a form that is hide and use it in other form with others in middle.

Like Form1 (value in textbox) then hide when Form2 show, -> Form2, Form3, in Form4 (take the value from Form1)...


Thanks a lot for you time!

You are most welcome...Cheers!

I have try with get and set but I didn't succeed :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.