Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to update a Parent Form from a child form?

Posted on 2004-08-26
4
Medium Priority
?
2,165 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 500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
Integration Management Part 2
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

926 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