Passing Data Between Forms in WinForms

I got a funny little email, it was a plea to answer a question that had gotten them stuck. They wanted to pass information between WinForms.

I have a value I want to set in another Windows Form. My radiobutton.checked needs to display on this form. It is a save settings form so value needs to go from parent form to child form. Please help. I tried to pass a value like an example taught but I get a error from Visual Studio. I am so stressed out and I got duplicate question on Stack Overflow but I still can’t get it to work.

Tom from a Community Email

I remember having this issue many moons ago. I figured that I would give a quick demo of passing data between two WinForms. It makes perfect sense once you understand what is going on. Lets dig into how to pass a value between WinForms.

WinForms Passing Data Demo

First make sure that you have two forms. Since the question specific addresses how to pass a radio button checked value to another form. We will do that.

Create a new project. I called mine PassingValues. In this example, I am using C#. Put two radio buttons on the form. I called mine, rbYes and rbNo with a button called btnOpenForm. Here is what form1 looks like.

The form that will do the data passing.
Make sure to select a default state or the form switch will always default to no on Form2

Form 2 will be a big old form with two radio buttons, also called rbYes and rbNo.

The Winform that is the data passing -ee?

Lets write the code for opening form2 when you press the button.

private void btnOpenForm_Click(object sender, EventArgs e)
{
    Form2 form2 = new Form2();
 
    form2.ShowDialog();
}

Currently, nothing get passed between the forms. The way you get data from one form to another is through the constructor of the child form. You must add a new parameter.

public Form2()
{
    InitializeComponent();
}
Before
public Form2(RadioButton rbFromForm1)
{
    InitializeComponent();
 
 
}
After

With that lets drop in some code that will allow us to pass the correct control. If I was doing such a binary thing, I would actually pass a boolean or make an enumerated type to pass through. I believe it is slightly more memory efficient to just do the most primitive datatype possible instead of a whole control object. I just wanted to show you could pass a big old object if you wanted. Lets look at what we have.

Form1

using System;
using System.Windows.Forms;
 
namespace PassingValues
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void btnOpenForm_Click(object sender, EventArgs e)
        {
            Form2 form2;
 
            if (rbYes.Checked)
            {
                form2 = new Form2(rbYes);
            }
            else
            {
                form2 = new Form2(rbNo);
            }
 
            form2.ShowDialog();
        }
    }
}

Form2

using System.Windows.Forms;
 
namespace PassingValues
{
    public partial class Form2 : Form
    {
        public Form2(RadioButton rbFromForm1)
        {
            InitializeComponent();
 
            if(rbFromForm1.Text == "Yes")
            {
                rbYes.Checked = true;
            }
            else
            {
                rbNo.Checked = true;
            }
        }
    }
}

Remember that forms are nothing more than objects. They have all the things an normal class would have. You instantiate it, you constructor it, you can write methods for it. As a result you can pass into it what you can pass into any function.

I hope this demonstrates this well enough, it will stick in your head. I know when the IDE does the work for me, I sometimes forget things like that fancy GUI thing it created is just a regular old object. Drop a comment below, if you want to see a Visual Basic implementation.

2 Comments

Leave a Reply

Your email address will not be published.