Jaa Zaib
Jaa Zaib

Reputation: 151

Form Close Issue in C#

I have a Login Form that is checking roles on login time. If role is Normal User it is opening Form1. If role is Register then it's opening Form2. After opening Form1 or Form2 i am closing login form using this.close. When role is Normal User it's opening Form1 and Closing Login Form Perfectly while when role is Register it's closing both forms.

Here is the code.

if (ROLE != "Register")
{
    Form1 form1= new Form1();
    if (ROLE == "Normal User")
    {
        form1.Show();
        this.Close();
    }
    if (ROLE == "Bulk User")
    {
        form1.Show();
        this.Close();
    }
}
else
{
    Form2 form2= new Form2();
    form2.Show();
    this.Close();
}

Login Form and Form1 are WPF Forms While Form2 is a Windows Form

Upvotes: 0

Views: 139

Answers (1)

Gevorg Narimanyan
Gevorg Narimanyan

Reputation: 296

That's because second form gets disposed when parent form getting closed, You can do following

else
{
      Form2 form2 = new Form2();
      form2.Show();
      this.Hide();
      form2.Closed += (s, args) => this.Close();
}

Upvotes: 4

Related Questions