Hello all,

I have recently ran into the issue of not knowing how to pass variables between forms.

Basically this is what I have:

Main_Form which calls Add_New_Student.

When a button (btn_Create_User) is pressed on Add_New_Student I want to close Add_New_Student and pass the variable to Main_Form.

I have had a look at a few tutorials but I really am not understanding much of it.

I was wandering if anyone could help me, I'm also new to these forums so if this post wasn't informative enough please let me know.

Thanks all!!

if your Main_Form is closed:-
you create a new object of Main_Form, pass the variable as a constructor paramter of the Main_Form object, close the Add_New_Student form and show the newly created Main_Form

if both your forms are open simultaneously:-
If you have a controller which holds references to objects of all the forms, you can simply set a property(public) of Main_Form through Add_New_Student form.

Lemme know if i make some sense

Both forms are open simultaneiously however I don't understand what you mean by controller. Sorry. If you could add a little more that would be great. Thanks.

k i'll copy paste some raw code first from one of my practice projects form olden days :)

 public static class Controller
    {
        //for each form controlled by this controller class,
        // add one variabl and one property
        private static frmViewStock fvs;
        private static frmStockPurchase fsp;

        //properties
        public static frmViewStock ViewStockForm
        {
            get
            {
                if (fvs == null || fvs.IsDisposed==true)
                {
                    fvs = new frmViewStock();
                }
                return fvs;
            }
            set
            {
                fvs = value;
            }
        }
        public static frmStockPurchase StockPurchaseForm
        {
            get
            {
                if (fsp == null || fsp.IsDisposed==true)
                {
                    fsp = new frmStockPurchase();
                }
                return fsp;
            }
            set
            {
                fsp = value;
            }
        }
}

So this static class holds the objects of all of your forms in your application. And you access all the forms through this controller class. Whenever you want to access any form you do something as:

frmStockPurchase fp = Controller.StockPurchaseForm;
fp.Show(); fp.Focus();

so if an object for StockPurchaseForm already exists in memory then the said form gains focus, else a new object is created and returned.

Now coming to passing values between forms, lets say i wanna pass a value for tax% to StockPurchaseForm. I'll create a private variable and a public property in StockPurchaseForm class, and then i have to do

fp.Tax_rate = 12.5;

----------------
hope it helps

There's really a lot of ways to do this. One option would be to use an event that other forms can subscribe to:

//In form two
public partial class Form2 : Form
{
    public class MyFormEventArgs : EventArgs
    {
        public string SomeData { get; set; }
        public MyFormEventArgs(string data)
        {
            SomeData = data;
        }
    }

    public delegate void MyFormEventHandler(object sender, MyFormEventArgs args);
    public event MyFormEventHandler DataUpdated;        

    private void button1_Click(object sender, EventArgs e)
    {
        DataUpdated(this, new MyFormEventArgs(textBox1.Text));
    }

    public Form2()
    {
        InitializeComponent();
    }
}

//In form 1, when you create a new Form2
Form2 myForm2 = new Form2();
myForm.DataUpdated += new Form2.MyFormEventHandler(myForm_DataUpdated);
//Then add a handler to Form1 that gets called whenever Form2 wants to send us some data (like after they press button1)
void myForm_DataUpdated(object sender, Form2.MyFormEventArgs args)
{
    this.textBox1.Text = args.SomeData;
}

Another option is to use public properties/member variables. Finally there's the option of static classes to hold global variables (which, in my opinion, is the worst method). Events are probably the best way to do this - it allows you to have multiple of the same form type communcating with other forms seamlessly without needing additional logic in a controller class to determine which form is actually talking. You can do this right from the event handler with the 'sender' object. Basically you can create a form, subscribe the nessecary events, then forget about it and let it take care of itself rather than managing references to each form you created.

Edited 4 Years Ago by skatamatic

This article has been dead for over six months. Start a new discussion instead.