Click here to Skip to main content
15,894,646 members
Articles / Programming Languages / C#

Using a delegate to pass data between two forms

Rate me:
Please Sign up or sign in to vote.
4.56/5 (36 votes)
18 Mar 2004 116.7K   3.7K   54  
Using a delegate to pass data between two forms
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Delegates
{
	/// <summary>
	/// Summary description for Form2.
	/// </summary>
	public class Form2 : System.Windows.Forms.Form
	{
        private System.Windows.Forms.Button btnForm2;
        private System.Windows.Forms.TextBox txtForm2;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

        // Defines a delegate. Sender is the object that is being returned to the other form.
        public delegate void PassControl(object sender);

        // Declare a new instance of the delegate (null)
        public PassControl passControl;


		public Form2()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

        public TextBox getText() 
        {
            return txtForm2;
        }
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.btnForm2 = new System.Windows.Forms.Button();
            this.txtForm2 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // btnForm2
            // 
            this.btnForm2.Location = new System.Drawing.Point(64, 104);
            this.btnForm2.Name = "btnForm2";
            this.btnForm2.Size = new System.Drawing.Size(144, 23);
            this.btnForm2.TabIndex = 0;
            this.btnForm2.Text = "Pass data and close form";
            this.btnForm2.Click += new System.EventHandler(this.btnForm2_Click);
            // 
            // txtForm2
            // 
            this.txtForm2.Location = new System.Drawing.Point(88, 48);
            this.txtForm2.Name = "txtForm2";
            this.txtForm2.TabIndex = 1;
            this.txtForm2.Text = "I am a TextBox";
            // 
            // Form2
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(272, 189);
            this.Controls.Add(this.txtForm2);
            this.Controls.Add(this.btnForm2);
            this.Name = "Form2";
            this.Text = "Form2";
            this.ResumeLayout(false);

        }
		#endregion


        private void btnForm2_Click(object sender, System.EventArgs e)
        {
            // If the delegate was instantiated, then call it
            if (passControl != null)
            {
                passControl(txtForm2);
            }

            // Close form
            this.Hide();        
        }
	}
}

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
Web Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions