Solved

How to update a Parent Form from a child form?

Posted on 2004-08-26
4
2,145 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

623 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