I am making a simple wizard. I created one static class for holding my forms:
namespace LABEL_AUTOMATION
{
static class ProjectHelper
{
public static Form1 frm1 = new Form1();
public static Form2 frm2 = new Form2();
}
}
and for form1’s next button I am having the following code:
private void buttonNext_Click(object sender, EventArgs e)
{
this.Visible = false;
ProjectHelper.frm2.Visible = true;
}
and similar logic for form2’s back button:
private void buttonPrevious_Click(object sender, EventArgs e)
{
this.Visible = false;
ProjectHelper.frm1.Visible = true;
}
The problem is that when I move between the forms, the values of the controls like textbox gets cleared.
How to correct it ?
Also, I am using the form’s visible changed event to execute the code when the user switches between forms. But, the event gets fired before the form gets visible. Any other event that I can use ?
EDIT: I actually didnt change the Program.cs file which was actually creating and running a seperate instance of Form1. So I changed my code to:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
ProjectHelper.frm1.Show();
}
But it is giving the following error:
The type initializer for 'LABEL_AUTOMATION.ProjectHelper' threw an exception.
I think you are showing/hiding instances of your forms which are not in
ProjectHelperclass.You should at Main do
or