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

Transferring information between two forms, Part 1: Parent to Child

Rate me:
Please Sign up or sign in to vote.
5.00/5 (19 votes)
3 Apr 2013CPOL4 min read 56.6K   1K   21   7
Questions here quite often ask "how do I send data from one form to another?" and I must have answered this a dozen times. So, a series of three tips to cover the process.

Introduction

It's a common problem. How do you transfer data between two forms in a Windows Forms application? And it's really easy to do. But...there are too many people out there that will tell you to just "make the control public" and set the value. Unfortunately that's a very bad idea - and the proper way is not a lot more complex.

This could have been an article, but I wanted to keep it focused (because this is information for a beginner  and felt that a tip for each of the common transfers might work better. 

So, a set of three tips:   

Part1: Parent to Child Transferring information between two forms, Part 1: Parent to Child[^]

  • Covers transferring data from a Parent form to it's Child form when the Parent want's to.
Part2: Child to Parent Transferring information between two forms, Part 2: Child to Parent[^
  • Covers transferring data from the Child to the Parent when the Child wants to. 
Part3: Child to Child Transferring information between two forms, Part 3: Child to Child[^
  • Covers transferring data from a Child to another Child when the first Child wants to. 

Background

If "making the control public" works, why is is it private by default? Why not do that and move on? It's easy to do and it works!

The answer is that when you do that, you are exposing the internals of your form to the whole world - and that is not a good idea, because:

  1. It's against the principles of OOPs
  2. It locks the design of the two forms together - you can't change one form without considering it's effects on the other.  Suppose you have a TextBox in a child, and  you make it public. You now can't change it to any other control (or do anything with it at all, pretty much) because it might break the form that is using it. This makes your life a lot harder when you have a small change to make, because suddenly it's a small change to the child, and a massive set of changes to the parent.
  3. When you make a control public, you don't just expose the field you want: myTextBox.Text. Oh, no - you expose them all. Font, Size, Location, BackColor, then lot. And there is nothing stopping any external class from changing them, while your form is running. Nasty...

So, instead, do it properly - it takes very little time and really does save you work later on! 

Using the code 

There are three basic ways that data needs to be transferred: this tip covers when the Parent wants to change data on a Child. This is the simplest form and can be done in two basic ways:

  1. Create a Child form constructor that accepts the data and pass it over when the form instance is created.
  2. Create a public property in the Child and pass the data via the instance.

The constructor method is trivial, and is not covered in the download.

Constructor method 

Just create a Child form constructor that takes the data as a parameter and use it to construct the instance:

Child Form 

C#
public frmChild (string data)
   {
   InitializeComponent();
   tbData.Text = data;
   } 
Parent Form
C#
frmChild child = new frmChild("Hello child!");
child.Show();

Property method 

This really isn't a lot more complex than the Constructor method. All you have to do is create a public property in the child that the parent can use to supply the data:

Child Form
C#
/// <summary>
/// Data to transfer into / out of form
/// </summary>
public string Data
    {
    get { return tbData.Text; }
    set { tbData.Text = value; }
    }
The Parent Form needs to save the instance at a class level, so it can talk to the child later on:
C#
/// <summary>
/// Instance of the child we send the data to.
/// </summary>
private frmChild child;
It can then create the form instance:
C#
child = new frmChild();
child.Data = "Hello from the Parent!";
child.Show();
When it wants to change the child data (from a button click or similar) it just uses the instance it saved, and copies the new data from it's source - in this case a text box:
C#
private void butTransfer_Click(object sender, EventArgs e)
    {
    if (child != null)
        {
        child.Data = tbParentData.Text;
        }
    }

The advantage of this method is that the data can move in both ways: the parent can set the data, and fetch it if it needs to, and you can change the child form to your hearts content - as long as it accepts and returns a string, the outside world does not need to know or care where it gets it from. 

History

  • 2013-02-18: First version.
  • 2013-02-19: Added links to other tips in series  
  • 2013-04-03: Corrected spelling error in title: "Transfering" should have been "Transferring" 
  • 2013-04-03 Corrected the same spelling error in the links to companion tips...  

License

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


Written By
CEO
Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?

Comments and Discussions

 
GeneralMy vote of 5 Pin
Sasuke Racher19-Mar-17 18:56
Sasuke Racher19-Mar-17 18:56 
QuestionTypo in title Pin
Thomas Daniels2-Apr-13 21:58
mentorThomas Daniels2-Apr-13 21:58 
AnswerRe: Typo in title Pin
OriginalGriff2-Apr-13 22:33
mveOriginalGriff2-Apr-13 22:33 
GeneralRe: Typo in title Pin
Thomas Daniels2-Apr-13 22:46
mentorThomas Daniels2-Apr-13 22:46 
GeneralRe: Typo in title Pin
Thomas Daniels2-Apr-13 22:49
mentorThomas Daniels2-Apr-13 22:49 
GeneralRe: Typo in title Pin
OriginalGriff2-Apr-13 23:11
mveOriginalGriff2-Apr-13 23:11 
GeneralRe: Typo in title Pin
Thomas Daniels2-Apr-13 23:14
mentorThomas Daniels2-Apr-13 23:14 

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.