Solved

How to update a Parent Form from a child form?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

861 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

22 Experts available now in Live!

Get 1:1 Help Now