我正在尝试为我将要发布的工具创建一个登录但是当我关闭子表单(Login)时,主要的父表单(工具)会打开。
在我的主要工具中,我在Form1_Load
:
Login Log = new Login();
Log.ShowDialog();
在子表单中,我为此示例创建了一个简单的登录表单:
if (metroTextBox1.Text == "User01") {
if (metroTextBox8.Text == "Password") {
this.Close();
}
else {
MessageBox.Show("Invalid Username/Password!");
}
}
else {
MessageBox.Show("Invalid Username/Password!");
}
在“退出按钮”下,我有这个:
this.Close();
Form1 Closer = new Form1();
Closer.Close();
但是当我按下退出按钮时,它会打开主要父表格,允许用户使用它而无需登录,这不是我想要的。
答案 0 :(得分:0)
实施到Form1:
public static Form1 MainFormForMySoftware;
当你创建Form1时,你说
MainFormForMySoftware = this;
当你想关闭父母时使用它:
MainFormForMySoftware.Close(); // It's a static reference for your Form1, you can close it from anywhere....
它将关闭父母甚至是孩子。
使用此代码:
this.Close();
Form1 Closer = new Form1();
Closer.Close();
你创建一个Form1的新实例,它不是父表单,这是一个新的form1 ......
答案 1 :(得分:0)
无论登录成功还是失败,您都需要某种方式与父表单进行通信。通常的方式是财产。在子表单中,添加属性
public bool Status
{ get; private set; }
然后在登录成功时设置Status = true;
,否则设置为Status = false;
。
您的主Form1_Load
变为:
Login log = new Login();
log.ShowDialog();
if (!log.Status)
{
Close(); // or whatever else you want to do after a failed login.
}
(请注意,变量名称以小写字母开头:log
。)