If you are working on a windows application, you may have have come accross a situation where you have to pass avariable between two forms.
Although there are many ways to do it, Sticking to my blog’s style, I will post the most easiest way to do it,
Steps to pass variables between forms
- Lets assume that the Form that is passing the variable as Form1 and the one recieving them as Form2
- Now Declare a public variable at the top of the form2 Class, so that it is global variable.
- Lets assume that variable to be an int variable X
- Now Initialise the Form2 class in Form1 as Form2 f2=new Form2()
- Now assign the value you want to pass to the Form2 to X like,
- f2.X=12; //You can pass any value here, make sure there is no data type mismatch
- after X is assigned, open the Form Form2 as f2.ShowDialoag();
- Now create an event for Form2 on load and Handle the Value of X
- eg-: texBox1.Text=X.ToString()
- You can now use the value of x in the Form2, you can use the same technique to pass any number of values, you can even use arrayas, objects, everything like this.