Solved

How to update a Parent Form from a child form?

Posted on 2004-08-26
4
2,130 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to host windows service on Windows Server 2008 3 43
Aspnet xml 2 36
What is MicroStrategy.NET? 2 47
Pass through dll 2 38
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now