Solved

How to update a Parent Form from a child form?

Posted on 2004-08-26
4
2,134 Views
Last Modified: 2012-05-07
The parent form is the main form and has a panel with labels, how do i update the label.text when i click on a button on the child form?
0
Comment
Question by:portilloleopoldo
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
Eamon21 earned 125 total points
ID: 11908737
You will need to provide a property in the parent form that can change the label.
e.g.

public class ParentForm: System.Windows.Forms.Form
      {
            private System.Windows.Forms.Label label1;

            public string LabelValue
            {
                  get
                  {
                        return this.label1.Text;
                  }
                  set
                  {
                        .label1.Text= value;
                  }
            }
      }

then from the child form:

if (this.Owner is ParentForm)
{
      ((ParentForm)this.Owner).LabelValue = @"Some label value";
}

This assumes that when you show the child form, you pass the ParentForm as the owner.

Eamon
0
 

Expert Comment

by:johnlewin
ID: 11908796
You need to pass a reference for the parent form to the child form, then use that reference from the child to update the parent. This can be accomplished a number of ways, but a simple example would be to pass the parent ref to the child via the childs construtor. For example:


using System;
using System.Drawing;
using System.Windows.Forms;

namespace temp
{
      /// <summary>
      /// Summary description for Parent.
      /// </summary>
      public class Parent: System.Windows.Forms.Form
      {
            public Parent()
            {
                  this.Text = "Original Title";
                  this.Size = new Size(500, 500);
                  this.BackColor = Color.LightGoldenrodYellow;
                  this.StartPosition = FormStartPosition.CenterScreen;
                  this.Click +=new EventHandler(Parent_Click);
                  this.Paint +=new PaintEventHandler(Parent_Paint);
            }

            private void Parent_Click(object sender, EventArgs e)
            {
                  // Pass a reference of this form to the child form
                  Child myChild = new Child(this);

                  myChild.ShowDialog();
            }

            private void Parent_Paint(object sender, PaintEventArgs e)
            {
                  e.Graphics.DrawString("Click me", Font, Brushes.Red, 20f, 20f);
            }
      }

      public class Child : System.Windows.Forms.Form
      {
            Form myParent;
            
            public Child(Form parent)
            {
                  // store the parent reference
                  myParent = parent;
                  
                  this.Size = new Size(400, 200);
                  this.Text = "Click form to update parent and close";
                  this.StartPosition = FormStartPosition.CenterScreen;

                  // Create an event handler for a general form click
                  this.Click +=new EventHandler(Child_Click);
            }

            private void Child_Click(object sender, EventArgs e)
            {
                  // Update parent
                  myParent.Text = "Hello World - " + DateTime.Now.ToString();
                  myParent.BackColor = Color.DeepSkyBlue;

                  // Exit the child form
                  this.Close();
            }
      }
}


0
 

Expert Comment

by:johnlewin
ID: 11908823
Ahh, after posting and reading the other new post, I need to clarify one point. Controls are private by default. So if you try to set the label.text value from the child form you'll hit an error. As Eamon21 demonstrates, the key is to expose access to private control properties, or the controls themselves through public or protected properties.
0
 
LVL 7

Expert Comment

by:NipNFriar_Tuck
ID: 11913758
Why not publish an event from the child for and catch it in the parent form?

i.e. Child Form:

public delegate void MyEventHandler( object sender, MyEventArgs mea );
public event MyEventHandler OnMyEvent;
private void FireEvent( string newLabel ) {
   if ( OnMyEvent != null ) {
      MyEventArgs mea = new MyEventArgs( newLabel );
      OnMyEvent( this, mea );
   }
}

Parent Form:

// when you create the child form subscribe to the event...
MyChild child = new MyChild();
child.OnMyEvent += new MyEventHandler( child_MyEvent );

public void child_MyEvent( object sender, MyEventArgs mea ) {
   label1.text = mea.NewLabel;
}

Event Class:

public class MyEventArgs : EventArgs {
   private string newLabel = String.Empty;
   public string NewLabel {
      get { return newLabel; }
   }
   public MyEventArgs( string label ) {
      newLabel = label;
   }
   }

}
}


0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question