简体   繁体   English

C#中的表单关闭问题

[英]Form Close Issue in C#

I have a Login Form that is checking roles on login time. 我有一个Login Form ,用于检查登录时间上的角色。 If role is Normal User it is opening Form1 . 如果角色是Normal User ,则它将打开Form1 If role is Register then it's opening Form2 . 如果角色是Register那么它将打开Form2 After opening Form1 or Form2 i am closing login form using this.close . 打开Form1Form2我将使用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. 当角色为Normal User它将完美地打开Form1并关闭登录表单;而当角色为Register它将同时关闭两个表单。

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 Login FormForm1是WPF表单,而Form2是Windows表单

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM