I run LoginForm using the code below:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new LoginForm());
//Application.Run(new MainForm());
}
When user presses the enter button, i want to dispose or close the LoginForm and want to run the MainForm but i got this exception: “InvalidOperationException was unhandled by user code”
Click handler of the enter button is given below:
private void LoginFormEnterButton_Click(object sender, EventArgs e)
{
try
{
MainForm a = new MainForm();
a.Show();
this.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
What can i do to dispose the LoginForm and after that to run the MainForm successfully?
You can’t dispose the Main window. You need to change the way.
First of all set MainForm as startup window.
Load/show the LoginForm in constructor of MainForm,
and Click handler code in LoginForm,