
In this Tutorial we'll be tackling a very common question amongst new .NET programmers, which is how to work with information across forms. What do I mean by "work across forms"? I mean using methods, variables, and control properties, from Form2 on Form1 or vise versus.
So lets get started!
#1 Setting up our forms for this tutorial.
Ok, to start, open up your C# IDE (I have Micrsoft Visual C# Express 2010), create a new form application, File > New Project > Windows Form Application. Now we need a second form to work with, so right click the solution and goto Add > Windows Form and leave the name as Form2.
(Tip: dont know what the solution is? Goto Project > Add Windows Form, at the top of the compiler)
Ok now that we have our forms setup lets add a button to each one. Form1 button should say "Not Clicked", Form2 button should say "Form1 Button Not Clicked". Alright now time for a little bit of code! So we need to show Form2 when we start the program. So lets create an instance of Form2 and call in subForm, then show it. To do this we add this code in Form1:
public Form1() { InitializeComponent(); subForm.Show();[COLOR=green]//This shows our form on start up.[/COLOR] } Form2 subForm = new Form2();[COLOR=green]//This creates a instance of Form2 so we may show it.[/COLOR]
#2 Calling a variable from Form2 on Form1.
Alright now that we have Form2 up and running when we start the program lets declare a variable in Form2, which im sure you know how to do.
public Form2() { InitializeComponent(); } public int Num = 1;Now lets call this variable in Form1 on button click. Double Click the button on Form1 and add this code:
MessageBox.Show(Convert.ToString(subForm.Num));[COLOR=green]//subForm is the instance of Form2 we created and 'Num' is variable we wanted to access.[/COLOR]You can also change the 'Num' variable from Form1 like this:
subForm.Num = 5;Now debug you application and see if it works.
#3 Calling a method from Form1 thats coded in Form2.
First lets add a method to Form2 that we can call from Form1. Add this code two lines below where we created the variable in Form2:
public void ChangeText() { button1.Text = "Form1 Button Clicked"; }Ok now we have a method that changes the text of the button, but we need to call it soooo back to Form1's button click event.
Now in Form1's button click we need to call the method like so:
subForm.ChangeText();[COLOR=green]//Once again 'subForm' is the Form2 instance we made, and 'ChangeText();' is that method we want to call.[/COLOR]Run your program and Form2's button text should change.
#4 Accessing information in Form1 from Form2.
Alright now we know how to access code in Form2 from Form1 lets do the opposite and learn how to access properties of controls, eg we'll change the button text in Form1 from Form2.
Now this is where it starts to get a little more complicated, but dont worry im sure you'll under stand. First lets goto the code of Form2, and declare an instance of Form1. So add this code directly under where we declared the 'int' variable earlier.
public Form1 mainForm;[COLOR=green]//Notice how this code is a little different? Its because where not creating a new instance of Form1, we just creating an instance or variable that needs to be assigned to Form1.[/COLOR]Now lets head back to Form1 and add some code. Ok so just above where we show Form2 were going to assign this 'mainForm' to Form1:
public Form1() { InitializeComponent(); subForm.mainForm = this;[COLOR=green]//Now we declare the 'mainForm' instance to 'this' which refers to the form your coding in, which in this case is Form1.[/COLOR] subForm.Show(); }Now that we can access information on Form1 from Form2, lets change Form1's button text via Form2's method.
In Form2's 'ChangeText' method lets add this code:
mainForm.button1.Text = "This Button Was Clicked";[COLOR=green]//We refer to 'mainForm' which is Form1, then to Form1's button text and change it.[/COLOR]Now you probably thinking what am i doing wrong?!? I have an error:
'WorkingAcrossForms.Form1.button1' is inaccessible due to its protection level'
This is because Form1's button properties are set to private. To fix this click the button on Form1 and open the properties window, scroll down until you see Modifiers and change it from 'Private' to 'Public'. Now run your application, and Form1's button text should change!
Well thats my tutorial, I hope you enjoyed it, and learned how to work across form, while still being able to understand clearly what was going on! +rep, comments, and questions welcome, I also attached the working project.
Good Day ~ Committed.

[ATTACH]3606[/ATTACH]
Attached Files
Edited by Alexander, 02 January 2011 - 03:22 AM.
( modified prefix )