Click here to Skip to main content
15,892,746 members
Articles / Programming Languages / C#

Passing Data between Windows Forms

Rate me:
Please Sign up or sign in to vote.
4.63/5 (86 votes)
27 Jan 20076 min read 318.5K   13.6K   231  
Passing Data between Windows Forms
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PassBetweenForms
{
    public partial class frmID : Form
    {

        // add a delegate
        public delegate void IdentityUpdateHandler(object sender, IdentityUpdateEventArgs e);

        // add an event of the delegate type
        public event IdentityUpdateHandler IdentityUpdated;


        // default constructor
        public frmID()
        {
            InitializeComponent();
        }

        // close the form without raising the event
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        // raise the event
        private void btnOkay_Click(object sender, EventArgs e)
        {
            // this button click event handler will raise the 
            // event which can then intercepted by any listeners

            // read the textboxes and set the member variables
            string sNewFirst = txtFirstName.Text;
            string sNewMiddle = txtMiddleName.Text;
            string sNewLast = txtLastName.Text;

            // instance the event args and pass it each value
            IdentityUpdateEventArgs args = new IdentityUpdateEventArgs(sNewFirst,
                sNewMiddle, sNewLast);

            // raise the event with the updated arguments
            IdentityUpdated(this, args);

            this.Dispose();
        }
    }


    public class IdentityUpdateEventArgs : System.EventArgs
    {
        // add local member variable to hold text
        private string mFirstName;
        private string mMiddleName;
        private string mLastName;

        // class constructor
        public IdentityUpdateEventArgs(string sFirstName, string sMiddleName, string sLastName)
        {
            this.mFirstName = sFirstName;
            this.mMiddleName = sMiddleName;
            this.mLastName = sLastName;
        }

        // Properties - Accessible by the listener

        public string FirstName
        {
            get
            {
                return mFirstName;
            }
        }

        public string MiddleName
        {
            get
            {
                return mMiddleName;
            }
        }


        public string LastName
        {
            get
            {
                return mLastName;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions