Click here to Skip to main content
11,813,974 members (54,233 online)
Click here to Skip to main content

Using a delegate to pass data between two forms

, 18 Mar 2004 77.5K 2.8K 54
Rate this:
Please Sign up or sign in to vote.
Using a delegate to pass data between two forms


This code demonstrates how you can pass data from one form to another using a delegate. The advantage of using a delegate is that the form from which you want to send the data, doesn't need to know anything about the form that it's sending its data to. This way, you can reuse the same form in other forms or applications.


This is form 1. From this form we display form 2. And from form 2 we send a TextBox back to form 1.

And the code for form 1:

private void btnForm1_Click(object sender, System.EventArgs e)
    // Create an instance of form 2
    Form2 form2 = new Form2();
    // Create an instance of the delegate
    form2.passControl = new Form2.PassControl(PassData);
    // Show form 2
private void PassData(object sender)
    // Set de text of the textbox to the value of the textbox of form 2
    txtForm1.Text = ((TextBox)sender).Text;

Form 2 sends the TextBox back to form 1:

public class Form2 : System.Windows.Forms.Form
    // Define delegate
    public delegate void PassControl(object sender);
    // Create instance (null)
    public PassControl passControl;
    private void btnForm2_Click(object sender, System.EventArgs e)
        if (passControl != null)
Of course, using the delegate, you can not only send back the textbox, but other controls, variables or even the form itself, because they are all objects. I hope this is useful.


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


About the Author

Web Developer
Netherlands Netherlands
No Biography provided

You may also be interested in...

Comments and Discussions

Generalthis is wondeful Pin
Member 1049412811-Mar-14 0:06
memberMember 1049412811-Mar-14 0:06 
QuestionNice one!!!! Pin
JOVOH20-Nov-12 4:16
memberJOVOH20-Nov-12 4:16 
GeneralMy vote of 5 Pin
karlosmunjos16-Apr-11 9:33
memberkarlosmunjos16-Apr-11 9:33 
GeneralThank you, good work ! Pin
zdavaz27-May-09 7:27
memberzdavaz27-May-09 7:27 
QuestionNull Reference Exception Pin
acezrwild8179-Jun-06 5:29
memberacezrwild8179-Jun-06 5:29 
GeneralCode in VB.NET Pin
New_Coder8-Jun-06 3:28
memberNew_Coder8-Jun-06 3:28 
Generalgood work Pin
MinaFawzi28-Oct-05 3:46
memberMinaFawzi28-Oct-05 3:46 
this is a very good work but u should lock the first form when the second one is active

GeneralIt's perfect! Pin
wenstar1813-Oct-05 1:15
memberwenstar1813-Oct-05 1:15 
GeneralResemble to RaiseEvent Pin
deasyhp2-Aug-04 20:53
memberdeasyhp2-Aug-04 20:53 
GeneralRe: Code in Pin
sasijrao18-May-04 5:12
membersasijrao18-May-04 5:12 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151002.1 | Last Updated 19 Mar 2004
Article Copyright 2004 by JasperB
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid