I have an MDI form "mdi1".
And another form "form1"
On mdi1 I have a label "lbl1"
I want to change text of lbl1 when a button on form1 is clicked.
form1 is a child form of mdi1.
How can i do this.

Recommended Answers

All 4 Replies

yse you can do this by using
MDIParent1.Label1.Text = TextBox1.Text
this on button click event

This reference might help. :twisted: Looks like it addresses exactly what you're asking for here with about 2 lines of code.

Hope this helps :) Please remember to mark this thread solved once your issue is resolved.

Form f = this.MdiParent;
            f.Controls["label1"].Text = "Hello MARKAND911";

This will solve your problem.Place the code inside the button Click event

Is it not bad OO design to directly access the parent controls from the child? Wouldnt using a property be a more appropriate method?
It maintains encapsulation and allows you to validate the value passed from the child:

public partial class Parent : Form
{
    //write-only property
    //could add get method to allow child to read from parent control
    public string TextBox1Value
    {
        set
        {
            //check input is valid
            if (value == "Valid")
                //write value to control
                TextBox1.Text = value;
        }
    }
}

public partial class Child : Form
{
    private void WriteToParent (string Text)
    {
        //retrieve parent and cast to parent type then send text to property
        ((Parent)this.MdiParent).TextBox1Value = "Valid";
    }
}
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.