Losing Your Forms
Let’s say we want to use the button on the Form2 instance to get back to our instance of Form1. Here’s where we
start to run into the limitations of instantiating a new instance of a form class. To fully illustrate the point,
add the symmetric code to the click event of the button on the Form2 instance to show an instance of Form1:
private void btnForm1_Click(object sender, System.EventArgs e) {
Form1 form1 = new Form1();
form1.Show();
}
Next, fire up the project. Enter some interesting phrase in the textbox in Form1, such as I think therefore I
exist! Click the button to load it into the label. Now click the button to show the instance of Form2. On Form2,
click the button to show an instance of Form1. No matter how many times you do this, all you’ll get is a new
instance of Form1, meaning the text that was loaded into the label by the user will not be displayed.
Often times, of course, there will be a lot more information—input by the user or gathered in some other
way—in a Windows form instance than a single phrase. So, how can we get back to the instance of Form1 that
we want to return to with its information intact?
There has to be an easy way to resolve this common (and very frustrating) programming problem. For the solution, read on.
|
|
Search Engine Optimization
 
Syndication Viewer
Our Web host:
IX WebHosting
|