Page 1 of 1

Passing Data Between Forms in C# How to pass data between two forms

#1 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Posted 05 December 2008 - 11:33 AM

Passing data between two forms seems to be one of the most common questions from beginners in .Net. There are a number of ways of accomplishing this task. Some are better than others, but none are wrong(as long as they work).

I will be doing this tutorial on the method of using the constructors to pass the data. I choose this method because, in my opinion, it is easier to understand, and it also uses less code.

I will be doing this tutorial using C#, .Net 2.0, and Visual Studio 2005.

So we will start by creating a new project in Visual Studio.

1. Open Visual Studio
2. File --> New --> Project
3. Visual C# --> Windows --> Windows Application
4. Give the project a descriptive name, or leave it as the default. I will be naming mine "PassingDataExample".
5. Click OK.

Next, I will rename the form to give it a more descriptive name. I will name it "frmParentForm".

Now, we will add controls to the form. I normally do this using code, but you can use the designer if you like.

Code for the Parent Form's local class variables for the textbox and button. Also the Parent Form's constructor, which is added by default, and the Parent Form's Form_Load event that will call a method to create the controls.
// If you use the designer to create the controls,
//    you will NOT need these two declarations
TextBox textBox1;
Button btnSendToNextForm;

public frmParentForm()
{
    InitializeComponent();
}

private void frmParentForm_Load(object sender, EventArgs e)
{
    // If you use the designer to create the controls,
    //    you will NOT need this method
    AddControlsToForm();
}



If you use the designer to create the controls, you will NOT need this method.
// Adds a textbox and button control to the form.
private void AddControlsToForm()
{
    textBox1 = new TextBox();
    textBox1.Name = "textBox1";
    textBox1.Location = new Point(91, 78);
    textBox1.Size = new Size(100, 20);

    btnSendToNextForm = new Button();
    btnSendToNextForm.Text = "Send";
    btnSendToNextForm.Name = "btnSendToNextForm";
    btnSendToNextForm.Location = new Point(101, 129);
    btnSendToNextForm.Size = new Size(75, 23);
    btnSendToNextForm.Click += new EventHandler(btnSendToNextForm_Click);

    this.Controls.Add(textBox1);
    this.Controls.Add(btnSendToNextForm);

    this.ActiveControl = textBox1;
}





Next, we are going to create a second form:
1. Project --> Add Windows Form
2. Rename the form. I am going to rename it to "frmChildForm".
3. Click Add.

I am going to re-use code from the Parent form's AddControlsToForm method(which a few changes). Again, if you want to use the designer to create the controls, knock yourself out.

Code for the Child Form's local class variables for the textbox and button. Also the Child Form's constructor, which is added by default. There is no Form_Load because it is not needed.
TextBox textBox1;
Button btnClose;

public frmChildForm()
{
    InitializeComponent();
}



If you use the designer to create the controls, you will NOT need this method. Notice the few differences.
// Adds a textbox and button control to the form.
private void AddControlsToForm()
{
    textBox1 = new TextBox();
    textBox1.Name = "textBox1";
    textBox1.Location = new Point(91, 78);
    textBox1.Size = new Size(100, 20);

    btnClose = new Button();
    btnClose.Text = "Close";
    btnClose.Name = "btnClose";
    btnClose.Location = new Point(101, 129);
    btnClose.Size = new Size(75, 23);
    btnClose.Click += new EventHandler(btnClose_Click);

    this.Controls.Add(textBox1);
    this.Controls.Add(btnClose);

    this.ActiveControl = textBox1;
}



Next, we are going to add a new constructor to the Child Form. The new constructor will accept a string value which will represent the text from the textbox on the Parent Form. We are also going to add a new declaration for a string variable, and set the text of the textbox to the string value.
TextBox textBox1;
Button btnClose;
[b][size=4]string textFromParent = string.Empty;[/size][/b]

public frmChildForm()
{
    InitializeComponent();
}

[b][size=4]public frmChildForm(string text)
{
    InitializeComponent();

    textFromParent = text;

    AddControlsToForm();

    textBox1.Text = textFromParent;
}[/size][/b]




Next, we are going to add code to the Click_Event of btnSendToNextForm on the Parent Form. This code will create an instance of the Child Form, and populate it's constructor with the value from the textbox. It will show it as a dialog box.
void btnSendToNextForm_Click(object sender, EventArgs e)
{
    frmChildForm newChildForm = new frmChildForm(textBox1.Text);
    newChildForm.ShowDialog();
    newChildForm.Dispose();
}  



And the last piece of code is the Click_Event of btnClose on the Child Form.
void btnClose_Click(object sender, EventArgs e)
{
    this.Close();
}




And there you go. You have now passed data between two forms.

I have also attached the solution.

Attached File(s)



Is This A Good Question/Topic? 2
  • +

Replies To: Passing Data Between Forms in C#

#2 genergabasa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-January 09

Posted 27 January 2009 - 11:19 PM

Nice tutorial. I just want to add that if you pass an object to another form eg.
ListBox listbox;
public frmChild(ListBox listbox)
{
	InitializeComponent(); // assume the form was created using the designer
	this.listbox = listbox;
}



and call it from the parent form eg.
private void btnParent_Click(object sender, EventArgs e)
{
   new frmChild(this.listBox1).Show();
}



and add items from the child eg.
private btnAdd_Click(object sender, EventArgs e)
{
	listbox.Items.Add(txtItemName.Text);
}



the listbox from the parent will be updated. This is because the listbox in the child is just a pointer to the listbox in the parent form.
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Posted 16 February 2009 - 09:09 AM

I need to update the code, but I can't seem to edit my post, so I will do it here. In my tutorial, I created the controls from the code behind. Because of this, I am not required to call the "InitializeComponent()" method. However, if you choose to use the designer for the controls, you will be required to call this method in the constructor.

to catch all instances, I am changing the code to include the method..

public frmChildForm(string text)
{
	InitializeComponent();

	textFromParent = text;

	AddControlsToForm();

	textBox1.Text = textFromParent;
}


Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Posted 16 February 2009 - 11:37 AM

I've update the original text to reflect the change.
Was This Post Helpful? 0
  • +
  • -

#5 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 359
  • View blog
  • Posts: 1,529
  • Joined: 08-April 09

Posted 09 June 2012 - 05:25 AM

How can you send data back from Child form to Parent?
Was This Post Helpful? 0
  • +
  • -

#6 Ajgor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-June 12

Posted 25 June 2012 - 12:38 AM

This is my first post here, so welcome.

View PostMichael26, on 09 June 2012 - 05:25 AM, said:

How can you send data back from Child form to Parent?


This is very simple. First of all, U have to create a public method in parent form, that U want to call from child form.
For example:


public void changeText(string s)
{
   label1.Text = s;
}




Then in the child form, U have to find the parent form handle, and use it to call the method:


   Form1 myParent = (Form1)this.ParentForm;  //Getting the parent form handle
   myParent.changeText("New text");          //Calling method in the parent form




Salute.
Ajgor
Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Posted 25 June 2012 - 05:05 AM

View PostMichael26, on 09 June 2012 - 08:25 AM, said:

How can you send data back from Child form to Parent?


This tutorial will show you what most people like to do. That is to use an event on the child form. The parent form would subscribe to that event.

http://www.dreaminco...-custom-events/
Was This Post Helpful? 1
  • +
  • -

#8 davidJB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-August 12

Posted 09 August 2012 - 07:47 AM

HI, I have worked through this example and it has been a great help to understand C#. However, I am getting an error when I don't have th form load function. The correction seems to be to add the method with any definition.

Can you explain what the problem is?

View PostdavidJB, on 09 August 2012 - 07:44 AM, said:

HI, I have worked through this example and it has been a great help to understand C#. However, I am getting an error when I don't have th form load function. The correction seems to be to add the method withoutany definition, so like this:
private void frmParent_Load(object sender, EventArgs e)
{

}

That'll fix it but can you explain what the problem is that forces me to add that?

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1