如何关闭当前窗体并同时打开另一个窗体 [英] How to close the current form and open another at the same time

查看:51
本文介绍了如何关闭当前窗体并同时打开另一个窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在我的项目中形成一个要连接的表单和我想要的主表单,当用户和密码为真时关闭当前登录表单并打开主表单,我创建了检查用户名的代码并且密码是真的,如果检查是真的,我使用了一个 IF 语句,但问题是当我按下连接按钮时,两个表单消失了这是我尝试使用的代码:

I have to forms in my project one form to connect and the main form I want when the user and the password are true to close the current login form and open the main form, I created the code which check if the user name and the password are true, and I used an IF statement if the check is true, but the problem is when I press the connect button the two forms are disappeared this is the code I tried to use :

if (allowCnx)
{
    Form1 mainForm = new Form1();
    mainForm.Show();
    this.Close();
 }

推荐答案

这里可能的问题是您可能将登录表单设置为主表单(或启动时运行的表单).当该表单关闭时,整个应用程序都会关闭.

The likely problem here is that you likely have your login form set as the main form (or the form that is run on startup). When that form is closed the entire application closes.

有多种解决方案,但我建议的一个是确保将 Form1 设置为实际的启动表单,然后在它的表单加载方法中具有如下内容:

There are a number of solutions, but one that I would suggest is to ensure that Form1 is set as the actual startup form, and then in it's form load method have something like this:

Hide();
LoginForm otherForm = new LoginForm();
otherForm.ShowDialog();
if(otherForm.Success)
{
    Show();
}
else
{
    Close();
}

这篇关于如何关闭当前窗体并同时打开另一个窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆