Click here to Skip to main content
15,880,891 members
Articles / Programming Languages / C#
Tip/Trick

Pass value between forms using events

Rate me:
Please Sign up or sign in to vote.
4.93/5 (29 votes)
22 May 2010CPOL 63.1K   24   10
The correct way to inform another object that something has happened or a value has changed.
This comes up so many times in the Forums/Quick Answers I thought it was time for a Tip that I can point people to.

Passing a value from a child form to the parent is best achieved using an event. To a noob, creating events, especialy when passing a value, can seem unnecessarily complicated. The code below shows a simple implementation that can be adapted as required.

The example is using Forms, but the priciple applies to any object type.

All the code is commented, any questions - just ask!
C#
// TextEventArgs.cs
using System;

public class TextEventArgs : EventArgs
{
    // Private field exposed by the Text property
    private string text;

    public TextEventArgs(string text)
    {
        this.text = text;
    }

    // Read only property.
    public string Text
    {
        get { return text; }
    }
}
C#
// Form2.cs
using System;
using System.Drawing;
using System.Windows.Forms;
public partial class Form2 : Form
{
    // Our event
    public event EventHandler<TextEventArgs> NewTextChanged;
    /* Private field to hold the value that is passed around.
     * Exposed by the NewText property and passed in the NewTextChanged event. */
    private string newText;
    /* The following line would normally be automatically created
     * in the Form1.Designer.cs file. */
    private TextBox textBox;
    public Form2()
    {
        InitializeComponent();
        /* The following lines would normally be automatically created
         * in the Form2.Designer.cs file. */
        textBox = new TextBox();
        textBox.Location = new Point(92, 122);
        textBox.TextChanged += new EventHandler(textBox_TextChanged);
        Controls.Add(textBox);
    }
    // textBox.TextChanged handler
    private void textBox_TextChanged(object sender, EventArgs e)
    {
        NewText = textBox.Text;
    }
    public string NewText
    {
        get { return newText; }
        /* Setting this property will raise the event if the value is different.
         * As this property has a public getter you can access it and raise the
         * event from any reference to this class. in this example it is set from
         * the textBox.TextChanged handler above. The setter can be marked as
         * as private if required. */
        set
        {
            if (newText != value)
            {
                newText = value;
                OnNewTextChanged(new TextEventArgs(newText));
            }
        }
    }
    // Standard event raising pattern.
    protected virtual void OnNewTextChanged(TextEventArgs e)
    {
        // Create a copy of the event to work with
        EventHandler<TextEventArgs> eh = NewTextChanged;
        /* If there are no subscribers, eh will be null so we need to check
         * to avoid a NullReferenceException. */
        if (eh != null)
            eh(this, e);
    }
}
C#
// Form1.cs
using System;
using System.Drawing;
using System.Windows.Forms;
public partial class Form1 : Form
{
    /* The following line would normally be automatically created
     * in the Form1.Designer.cs file */
    private Button buttonShowForm2;
    public Form1()
    {
        InitializeComponent();
        /* The following lines would normally be automatically created
         * in the Form1.Designer.cs file */
        buttonShowForm2 = new Button();
        buttonShowForm2.Location = new Point(105, 121);
        buttonShowForm2.Text = "Show Form2";
        buttonShowForm2.Click += new EventHandler(buttonShowForm2_Click);
        Controls.Add(buttonShowForm2);
    }
    private void buttonShowForm2_Click(object sender, EventArgs e)
    {
        Form2 form2 = new Form2();
        // Subscribe to event
        form2.NewTextChanged += new EventHandler<TextEventArgs>(form2_NewTextChanged);
        form2.ShowDialog();
        // Unsubscribe from event
        form2.NewTextChanged -= form2_NewTextChanged;
        form2.Dispose();
        form2 = null;
    }
    // Our new event (form2.NewTextChanged) handler
    private void form2_NewTextChanged(object sender, TextEventArgs e)
    {
        Text = e.Text;
    }
}

License

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


Written By
CEO Dave Meadowcroft
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 4 Pin
Member 108252156-Apr-15 21:20
Member 108252156-Apr-15 21:20 
QuestionMy Vote of 5 Pin
adils.kiet7-Jul-13 21:22
adils.kiet7-Jul-13 21:22 
Question[My vote of 1] Confused - My vote of Zero. Pin
Charles Shob26-Sep-12 18:23
professionalCharles Shob26-Sep-12 18:23 
AnswerRe: [My vote of 1] Confused - My vote of Zero. Pin
DaveyM6929-Sep-12 12:59
professionalDaveyM6929-Sep-12 12:59 
GeneralMy vote of 5 Pin
fjdiewornncalwe26-Sep-12 4:16
professionalfjdiewornncalwe26-Sep-12 4:16 
GeneralReason for my vote of 5 Nice and loosely coupled, my 5! Pin
jim lahey1-Feb-12 23:34
jim lahey1-Feb-12 23:34 
GeneralReason for my vote of 5 As one gets into programming models,... Pin
Your Display Name Here11-Aug-10 3:48
Your Display Name Here11-Aug-10 3:48 
Reason for my vote of 5
As one gets into programming models, the deeper we go, the more events raise their first class status. We become aware of their benefits and wonder why we still insist on Waterfall methodology.
GeneralReason for my vote of 5 Thanks for sharing this tip. Pin
linuxjr5-Aug-10 17:26
professionallinuxjr5-Aug-10 17:26 
Generalloser Pin
Barbo1-Sep-10 23:09
Barbo1-Sep-10 23:09 
GeneralRe: loser Pin
Carsten V2.01-Oct-12 8:44
Carsten V2.01-Oct-12 8:44 

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.