关闭C#Windows窗体时出现问题

时间:2021-04-13 15:53:20

I am having problems closing out a C# windows form app. It will currently just give me a blank form with no title or anything. I need to find a way to close this little unknown window.

我在关闭C#windows表单应用程序时遇到问题。它目前只会给我一张没有标题或任何内容的空白表格。我需要找到一种方法来关闭这个未知的小窗口。

I Have 2 form pages one for a login screen and one for the actual app. All being run by a program.cs file.

我有两个表单页面,一个用于登录屏幕,另一个用于实际应用程序。全部由program.cs文件运行。

program.cs

...
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new PROG1());
}

This is just the basic main created by visual studio to run my program.

这只是visual studio运行我的程序所创建的基本主要内容。

Then we have the main program

然后我们有主程序

PROG1.cs

...
public PROG1()
{
    Login LoginForm = new Login();
    DialogResult a = LoginForm.ShowDialog();

    if(LoginForm.ValidLogin == 1) {
        InitializeComponent();
    } else {
        Application.Exit(); //FAIL
    }
}

You can see that the first the program.cs file calls PROG1.cs which calls an instance of login.cs. If you insert a valid login the login page will close and the main PROG1 will show as it should. However if you simply click the red X to close login form it comes to Prog1 and LoginForm.ValidLogin != 1 so it does not Initialize the form and will try to close the form. This will just leave me the uninitialized form instead of closing it out. If I do a this.close() instead it will give me a runtime error.

您可以看到第一个program.cs文件调用调用login.cs实例的PROG1.cs。如果您插入有效的登录页面,登录页面将关闭,主PROG1将按原样显示。但是,如果您只需单击红色X以关闭登录表单,它将转到Prog1和LoginForm.ValidLogin!= 1,因此它不会初始化表单并尝试关闭表单。这将使我保持未初始化的形式,而不是将其关闭。如果我执行this.close()而不是它会给我一个运行时错误。

Any Ideas?

4 个解决方案

#1


Put the InitializeComponent call back at the top where it used to be. An attempt to use uninitialized variables, including trying to tell them to close themselves, is a bad idea.

将InitializeComponent调用放回原来的顶部。试图使用未初始化的变量,包括试图告诉他们自己关闭,这是一个坏主意。

Configure the properties of your PROG1 form so the initial state will be hidden instead of shown. After the LoginForm returns, your PROG1 code can decide whether to show itself or close itself.

配置PROG1表单的属性,以便隐藏而不是显示初始状态。在LoginForm返回后,您的PROG1代码可以决定是显示自身还是关闭自身。

#2


Is the reason you're not initializing the form on failure because the contents of the InitializeComponent() method take a very long time to run? It seems like you should just simplify your program to this:

你是不是在失败时初始化表单的原因是因为InitializeComponent()方法的内容需要很长时间才能运行?看起来你应该简化你的程序:

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    LoginForm login = new LoginForm();

    if (login.ShowDialog() == DialogResult.OK &&
        login.ValidLogin == 1)
    Application.Run(new PROG1());
}

And remove the logic from the constructor of PROG1.

并从PROG1的构造函数中删除逻辑。

#3


You shouldn't be doing something like this in the constructor.

你不应该在构造函数中做这样的事情。

Either have a new entry form that shows little or nothing and presents the login form on the Load event or from the Load event on the main form. If you show a modal dialog they cannot access your parent form and you can just close the parent if they fail to log in.

要么有一个新的条目表单显示很少或什么都没有,并在Load事件或主窗体上的Load事件中显示登录表单。如果您显示模式对话框,则他们无法访问您的父表单,如果他们无法登录,您可以关闭父表单。

#4


I would recommend the best practice as Samuel suggests. But if you want something quick, try moving the login logic to the Visual Studio's generated Main:

我推荐塞缪尔建议的最佳做法。但是如果你想要快速的东西,尝试将登录逻辑移动到Visual Studio生成的Main:

In Program.cs

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        Form2 login = new Form2();
        if (login.ShowDialog() == DialogResult.Yes)
        {
            Application.Run(new Form1());
        }
    }

In Form2.cs

    private void button1_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.No;
        this.Close();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.Yes;
        this.Close();
    }

#1


Put the InitializeComponent call back at the top where it used to be. An attempt to use uninitialized variables, including trying to tell them to close themselves, is a bad idea.

将InitializeComponent调用放回原来的顶部。试图使用未初始化的变量,包括试图告诉他们自己关闭,这是一个坏主意。

Configure the properties of your PROG1 form so the initial state will be hidden instead of shown. After the LoginForm returns, your PROG1 code can decide whether to show itself or close itself.

配置PROG1表单的属性,以便隐藏而不是显示初始状态。在LoginForm返回后,您的PROG1代码可以决定是显示自身还是关闭自身。

#2


Is the reason you're not initializing the form on failure because the contents of the InitializeComponent() method take a very long time to run? It seems like you should just simplify your program to this:

你是不是在失败时初始化表单的原因是因为InitializeComponent()方法的内容需要很长时间才能运行?看起来你应该简化你的程序:

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    LoginForm login = new LoginForm();

    if (login.ShowDialog() == DialogResult.OK &&
        login.ValidLogin == 1)
    Application.Run(new PROG1());
}

And remove the logic from the constructor of PROG1.

并从PROG1的构造函数中删除逻辑。

#3


You shouldn't be doing something like this in the constructor.

你不应该在构造函数中做这样的事情。

Either have a new entry form that shows little or nothing and presents the login form on the Load event or from the Load event on the main form. If you show a modal dialog they cannot access your parent form and you can just close the parent if they fail to log in.

要么有一个新的条目表单显示很少或什么都没有,并在Load事件或主窗体上的Load事件中显示登录表单。如果您显示模式对话框,则他们无法访问您的父表单,如果他们无法登录,您可以关闭父表单。

#4


I would recommend the best practice as Samuel suggests. But if you want something quick, try moving the login logic to the Visual Studio's generated Main:

我推荐塞缪尔建议的最佳做法。但是如果你想要快速的东西,尝试将登录逻辑移动到Visual Studio生成的Main:

In Program.cs

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        Form2 login = new Form2();
        if (login.ShowDialog() == DialogResult.Yes)
        {
            Application.Run(new Form1());
        }
    }

In Form2.cs

    private void button1_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.No;
        this.Close();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.Yes;
        this.Close();
    }