Click here to Skip to main content
15,867,141 members
Articles / Desktop Programming / Windows Forms
Article

Passing Values between Forms in .NET 1.x with C# and VB.NET examples

Rate me:
Please Sign up or sign in to vote.
4.71/5 (74 votes)
10 Nov 2005CPOL6 min read 368.3K   4.5K   98   61
This article provides an introduction to passing values between two forms in an application. The examples are written in C# and VB.NET and are targeted at .NET 1.1.

Introduction

I wrote this article in response to an almost overwhelming number of requests on forums on how to pass a variable from a one Windows Form to another. In fact, saying that it is going between one form and another is somewhat of a misnomer because this will work for any type of object not just Forms. This seems to be one area where beginners in C# and VB.NET often get stuck. So, rather than having to repeat myself often in forums I wrote this article so that I could point people at it so that they will have the benefit of a full explanation as well as the answer to their problem.

The code examples that come with this article are available for VB.NET and C# so that neither group feels left out. If you develop software in any of the other languages available for .NET then you can probably follow on too. The techniques are the same for all .NET languages the only difference should be in the language syntax.

Parent to child

Passing a value from a parent to a child class is hopefully one of the easiest techniques and is the one I'll start with. The idea is simple. The parent class needs to pass some value to the child class. In this example, I have set up a simple parent form with a TextBox control where the user can type any value they like, then they can open the child form and see that value appear there.

First of all, the child class needs some way to receive the value from the parent. I've set up a property to receive the value and it will set the Text value of a TextBox on the child form. In reality this could set a field value or get passed to another object and so on.

C#

C#
public string ValueFromParent
{
    set
    {
        this.uxParentValue.Text = value;
    }
}

VB.NET

VB
Public WriteOnly Property ValueFromParent() As String
    Set(ByVal Value As String)
        Me.uxParentValue.Text = Value
    End Set
End Property

To make the initialization of the class easier, the constructor will take a parameter which will be the current value to be passed. The constructor uses the property so that the code can be reused. Admittedly, in this example there is only one line in the property, but as an element of future proofing it means that there is the possibility of adding more functionality to the property in future should it need. That additional functionality will be implemented without having to change a lot of existing code.

C#

C#
public ChildForm(string initialValue)
{
    InitializeComponent();
    ValueFromParent = initialValue;
}

VB.NET

VB
Public Sub New(ByVal initialValue As String)
    MyBase.New()
 
    'This call is required by the Windows Form Designer.
    InitializeComponent()
 
    ValueFromParent = initialValue
End Sub

Now, in order for the parent to pass the value to the child, it only has to set the property. For example, when using the child form as a dialog the value from the parent can be passed as follows:

C#

C#
private void uxOpenDialog_Click(object sender, System.EventArgs e)
{
    // Get the value to be passed
    string parentValue = this.uxUserResponse.Text;
 
    // Create the child form.
    ChildForm child = new ChildForm(parentValue);

    // Show the form which will display the user's value.
    child.ShowDialog();
}

VB.NET

VB
Private Sub uxOpenDialog_Click(ByVal sender As System.Object, 
        ByVal e As System.EventArgs) Handles uxOpenDialog.Click
         
        ' Get the value that the child will be initialised with
        Dim initialValue As String
        initialValue = Me.uxUserResponse.Text
 
        Dim childForm As ChildForm
 
        ' Create the child form.
        childForm = New ChildForm(initialValue)
 
        ' Show the child dialog.
        childForm.ShowDialog()
End Sub

Of course, it may be necessary to pass a value when the child form is already open. This is also possible if the parent form stores a reference to the child form and then uses that to pass updates to the child form using the property in the child form that was set up earlier.

In my example, the parent is going to store a reference to the child as a field in the ParentForm class like this:

C#

C#
private ChildForm uxChildForm;

VB.NET

VB
Private uxChildForm As ChildForm

When creating the child form it is therefore important to pass the reference to the child form into the field variable. For example:

C#

C#
private void uxOpenForm_Click(object sender, System.EventArgs e)
{
    this.uxChildForm = new ChildForm();
    this.uxChildForm.ValueFromParent = this.uxUserResponse.Text;
    this.uxChildForm.Show();
}

VB.NET

VB
Private Sub uxOpenForm_Click(ByVal sender As System.Object, 
        ByVal e As System.EventArgs) Handles uxOpenForm.Click
    Me.uxChildForm = New ChildForm
    Me.uxChildForm.ValueFromParent = Me.uxUserResponse.Text
    Me.uxChildForm.Show()
End Sub

Finally, whenever the parent needs to tell the child that the value is updated it can do so just by passing the new value to the property that was set up in the child form. For example:

C#

C#
private void uxUserResponse_TextChanged(object sender, System.EventArgs e)
{
    if (this.uxChildForm != null)
    {
        this.uxChildForm.ValueFromParent = this.uxUserResponse.Text;
    }
}

VB.NET

VB
Private Sub uxUserResponse_TextChanged(ByVal sender As System.Object, 
        ByVal e As System.EventArgs) Handles uxUserResponse.TextChanged
    If Not Me.uxChildForm Is Nothing Then
        Me.uxChildForm.ValueFromParent = Me.uxUserResponse.Text
    End If
End Sub

The code for this is in the zip file above. The projects are ParentToChildCS (for C# developers) and ParentToChildVB (for VB developers).

Sibling to Sibling

The next most common request seems to be passing a value between two siblings. That is, two child classes sharing the same parent class. This also demonstrates passing between the child and the parent.

First, it is important to note that in most cases the two child classes should probably know nothing about each other. If the two child classes start to communicate directly with each other the coupling in the application increases and this will make maintenance of the application more difficult. Therefore the first thing to do is to allow the child classes to raise events when they change.

In order to raise events, create a delegate to define the signature of the method that will be called when the event is raised. It is a good practice to conform to the same style as the delegates used in the .NET Framework itself. For example, if an event handler for the Click event of a Button is created then it is possible to see that the signature of the method created by the IDE has two parameters, sender and e. sender is an object and is a reference to the object that generated the event. e is the EventArgs (or a something derived from EventArgs).

With that in mind, the delegate created here will have sender and e parameters as well. But for the e parameter a new class will be created to store the specific details of the event.

C#

C#
public class ValueUpdatedEventArgs : System.EventArgs
{
    // Stores the new value.
    private string newValue;
 
    // Create a new instance of the 
    // ValueUpdatedEventArgs class.
    // newValue represents the change to the value.
    public ValueUpdatedEventArgs(string newValue)
    {
        this.newValue = newValue;
    }
 
    // Gets the updated value.
    public string NewValue
    {
        get
        {
            return this.newValue;
        }
    }
}

VB.NET

VB
Public Class ValueUpdatedEventArgs
    Inherits System.EventArgs
 
    ' Stores the new value
    Dim _newValue As String
 
    ' Create a new instance of the 
    ' ValueUpdatedEventArgs class.
    ' newValue represents the change to the value.
    Public Sub New(ByVal newValue As String)
        MyBase.New()
        Me._newValue = newValue
    End Sub

    ' Gets the updated value
    ReadOnly Property NewValue() As String
        Get
            Return Me._newValue
        End Get
    End Property
 
End Class

Next the delegate needs to be defined.

C#

C#
public delegate void ValueUpdated(object sender, 
    ValueUpdatedEventArgs e);

VB.NET

VB
Public Delegate Sub ValueUpdated(ByVal sender As Object, 
    ByVal e As ValueUpdatedEventArgs)

Now that the foundation of the mechanism for informing other objects of changes is written, the child classes can implement the raising of events so that observers (i.e. interested parties) can be notified when the value changes.

In the class for the child forms the event ValueUpdated is declared so that interested parties can indicate what they want to observe.

C#

C#
public event ValueUpdated ValueUpdated;

This is one area where C# and VB.NET differ. In C# an event requires a delegate, whereas in VB.NET the signature of the method to be called needs to be declared as part of the event. The advantage for C# is that the delegate only has to be declared once and it can be reused in many places whereas VB.NET has to have essentially the same bit of code, the method signature, rewritten frequently - this can be an area where there can be discrepancies between one class and another.

Next, when the child class is updated it needs to inform its observers (the interested parties, in this case it will be the parent and sibling forms). The following code shows what happens when the text is changed on the form.

C#

C#
private void uxMyValue_TextChanged(object sender, System.EventArgs e)
{
    // Get the new value from the text box
    string newValue = this.uxMyValue.Text;
 
    // Create the event args object.
    ValueUpdatedEventArgs valueArgs = 
        new ValueUpdatedEventArgs(newValue);
 
    // Inform the observers.
    ValueUpdated(this, valueArgs);
}

VB.NET

VB
Private Sub uxMyValue_TextChanged(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles uxMyValue.TextChanged
    Dim newValue As String
    newValue = Me.uxMyValue.Text
    Dim valueArgs As ValueUpdatedEventArgs
    valueArgs = New ValueUpdatedEventArgs(newValue)
    RaiseEvent ValueUpdated(Me, valueArgs)
End Sub

Now that the child form can inform others of the changes and what the changes are, the parent needs to open the child form and register as an observer so that it can be informed when changes are made. The parent will also ensure that other siblings that are interested get added as observers too.

C#

C#
private void uxOpenChildA_Click(object sender, System.EventArgs e)
{
    // Create the child form
    this.childA = new ChildAForm();
 
    // Add an event so that the parent form is informed when the child
    // is updated.
    this.childA.ValueUpdated += new ValueUpdated(childA_ValueUpdated);
 
    // Add an event to each child form so that they are notified of each
    // others changes.
    if (this.childB != null)
    {
        this.childA.ValueUpdated += 
            new ValueUpdated(this.childB.childA_ValueUpdated);
        this.childB.ValueUpdated += 
            new ValueUpdated(this.childA.childB_ValueUpdated);
    }
 
    this.childA.Show();
}

VB.NET

VB
Private Sub uxOpenChildA_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles uxOpenChildA.Click
    ' Create the child form
    Me.childA = New ChildAForm
 
    ' Add a handler to this class for when child A is updated
    AddHandler childA.ValueUpdated, AddressOf ChildAValueUpdated
 
    If Not Me.childB Is Nothing Then
        ' Make sure that the siblings are informed of each other.
        AddHandler childA.ValueUpdated, _
            AddressOf childB.ChildAValueUpdated
        AddHandler childB.ValueUpdated, _
            AddressOf childA.ChildBValueUpdated
    End If
 
    ' Show the form
    Me.childA.Show()
End Sub

All that remains is to implement the event handlers.

C#

C#
private void childA_ValueUpdated(object sender, ValueUpdatedEventArgs e)
{
    // Update the text box on this form (the parent) with the new 
    /// value from the child
    this.uxChildAValue.Text = e.NewValue;
}

VB.NET

VB
Private Sub ChildAValueUpdated(ByVal sender As Object, 
        ByVal e As ValueUpdatedEventArgs) Handles childA.ValueUpdated
    ' Update the value on this form (the parent) with the 
    ' value from the child.
    Me.uxChildAValue.Text = e.NewValue
End Sub

And similarly in the child class.

C#

C#
public void childB_ValueUpdated(object sender, ValueUpdatedEventArgs e)
{
    // Update the text box on this form (child A) with the new value
    this.uxOtherValue.Text = e.NewValue;
}

VB.NET

VB
Public Sub ChildBValueUpdated(ByVal sender As Object, _
        ByVal e As ValueUpdatedEventArgs)
    Me.uxOtherValue.Text = e.NewValue
End Sub

The code for this example is in the accompanying zip file. The project is called SiblingToSiblingCS (for the C# version) and SiblingToSiblinVB (for the VB.NET version)

Conclusion

It is quite simple to pass values from one object to another. In some instances it does take a little bit of effort to get it done right. Especially as it is all too easy to couple forms together that really shouldn't know about each other.

History

  • 10-Nov-2005: V1.00 - Initial version.
  • 12-Nov-2005: V1.01 - Minor updates.

©2005 Colin Angus Mackay. All Rights Reserved - The only authorized websites that may show this article are CodeProject, ColinMackay.NET and Developer fusion.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
Scotland Scotland
Have been a Code Project MVP 5 years running and was Microsoft C# MVP 4 years running, MBCS, MIAP and a whole bunch of other stuff. Now I just help run Scottish Developers which is a user group with software development events in Edinburgh, Glasgow and Dundee and I have also started an open source project to help with Password Validation

Main topics I blog about:
* Parallelization in .NET
* Code Quality
* Data Security

Comments and Discussions

 
QuestionThanks a ton!!! Got something to ask :) Pin
Member 1122898012-Nov-14 11:31
Member 1122898012-Nov-14 11:31 
QuestionUsing the code for MDI forms Pin
Member 108683889-Jun-14 6:24
Member 108683889-Jun-14 6:24 
GeneralMy vote of 5 Pin
Blargocrappenstein20-May-14 11:43
Blargocrappenstein20-May-14 11:43 
GeneralMy vote of 5 Pin
igetorix2-Jan-13 4:43
igetorix2-Jan-13 4:43 
QuestionThanks Pin
Sean Archer13-Sep-12 21:47
Sean Archer13-Sep-12 21:47 
AnswerRe: Thanks Pin
Member 1122898012-Nov-14 11:35
Member 1122898012-Nov-14 11:35 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey9-Apr-12 18:30
professionalManoj Kumar Choubey9-Apr-12 18:30 
GeneralMy vote of 5 Pin
polczym18-Apr-11 14:16
polczym18-Apr-11 14:16 
GeneralTAN FACIL QUE SE HACIA EN VB6, PORQUE COMPLICARLO TANTO Pin
RAN30013-Mar-11 17:24
RAN30013-Mar-11 17:24 
GeneralMy vote of 4 Pin
Alberto Vida13-Dec-10 16:26
Alberto Vida13-Dec-10 16:26 
GeneralGood Job Pin
Mike Hankey10-Aug-10 12:00
mveMike Hankey10-Aug-10 12:00 
GeneralThanks Pin
wasimmn21-Jun-09 5:07
wasimmn21-Jun-09 5:07 
Questionthe other way around? Pin
akosikris6-Oct-08 1:53
akosikris6-Oct-08 1:53 
AnswerRe: the other way around? Pin
Colin Angus Mackay6-Oct-08 2:03
Colin Angus Mackay6-Oct-08 2:03 
GeneralRe: the other way around? Pin
akosikris6-Oct-08 2:08
akosikris6-Oct-08 2:08 
GeneralThanks! (and a code mismatch) Pin
Mike Brady1-Aug-08 19:01
Mike Brady1-Aug-08 19:01 
GeneralPassing Value from PopUpWindow.aspx to ParentPage.aspx Pin
Abu-Muawiyah21-May-08 19:55
Abu-Muawiyah21-May-08 19:55 
Generalreload the parent form after closing the child form Pin
parish21-Apr-08 6:48
parish21-Apr-08 6:48 
GeneralRe: reload the parent form after closing the child form Pin
Colin Angus Mackay21-Apr-08 7:19
Colin Angus Mackay21-Apr-08 7:19 
Questionhow to take value from a form say form1 .close form 1.now i want the value on form 2 Pin
kk_upadhyay15-Nov-07 21:44
kk_upadhyay15-Nov-07 21:44 
AnswerRe: how to take value from a form say form1 .close form 1.now i want the value on form 2 Pin
Colin Angus Mackay16-Nov-07 6:40
Colin Angus Mackay16-Nov-07 6:40 
Generalmethod Pin
Xmen Real 4-Nov-07 15:21
professional Xmen Real 4-Nov-07 15:21 
GeneralRe: method Pin
Colin Angus Mackay4-Nov-07 21:19
Colin Angus Mackay4-Nov-07 21:19 
GeneralRe: method Pin
Xmen Real 4-Nov-07 21:46
professional Xmen Real 4-Nov-07 21:46 
General.Net 2.0 Pin
Giorgi Dalakishvili29-May-07 20:51
mentorGiorgi Dalakishvili29-May-07 20:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.