change label text of parent form from child form

13,556

Solution 1

when calling the child form, set the Parent property of child form object like this..

Test1Form test1 = new Test1Form();
test1.Show(this);

On your parent form, make the property of your label text like as..

public string LabelText
{
  get
  {
    return  Label1.Text;
  }
  set
  {
    Label1.Text = value;
  }
}

From your child form you can get the label text like that..

((Form1)this.Owner).LabelText = "Your Text";

Solution 2

No doubt there are plenty of short cut ways to do this, but in my view a good approach would be to raise an event from the child form that requests that the parent form changes the displayed text. The parent form should register for this event when the child is created and can then respond to it by actually setting the text.

So in code this would look something like this:

public delegate void RequestLabelTextChangeDelegate(string newText);

public partial class Form2 : Form
{
    public event RequestLabelTextChangeDelegate RequestLabelTextChange;

    private void button1_Click(object sender, EventArgs e)
    {
        if (RequestLabelTextChange != null)
        {
            RequestLabelTextChange("Bye");
        }
    }        

    public Form2()
    {
        InitializeComponent();
    }
}


public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.RequestLabelTextChange += f2_RequestLabelTextChange;
    }

    void f2_RequestLabelTextChange(string newText)
    {
        label1.Text = newText;
    }
}  

Its a bit more long winded but it de-couples your child form from having any knowledge of its parent. This is a good pattern for re-usability as it means the child form could be used again in another host (that doesn't have a label) without breaking.

Share:
13,556

Related videos on Youtube

Milind
Author by

Milind

Updated on September 15, 2022

Comments

  • Milind
    Milind over 1 year

    Possible Duplicate:
    accessing controls on parentform from childform

    I have parent form form1 and child form test1 i want to change label text of parent form from child form in my parent form i have method to showresult()

    public void ShowResult() { label1.Text="hello"; }

    I want to change label.Text="Bye"; form my child form test1 on button click event. Please give any suggestions.

  • Milind
    Milind over 11 years
    is it necessary to create new object of parent form ?
  • Milind
    Milind over 11 years
    ((Form1)this.ParentForm) after that label1 is not accesible
  • Talha
    Talha over 11 years
    @Milind check the updated answer
  • Milind
    Milind over 11 years
    from my child form should i create new object of parent form and then access label control with that new object?
  • Milind
    Milind over 11 years
    i use labelText property in parent form and it is now accesible in child form but at runtime it throws exception "Object reference not set to an instance of an object."
  • Talha
    Talha over 11 years
    @Milind check the updated answer
  • Hamlet Hakobyan
    Hamlet Hakobyan over 11 years
    @Milind, certainly not, you must pass parent form (form1) as owner of child form (test1), then use them from child form.
  • Milind
    Milind over 11 years
    at time of calling child form Test1Form test1 = new Test1Form(); test1.Parent = this; it show error "Top-level control cannot be added to a control."
  • Talha
    Talha over 11 years
    @Milind ok leave it, use the Owner property of form, and call child form as test1.Show(this);
  • Jarkid
    Jarkid over 6 years
    The key is to put "this" in Show: test1.Show(this);