C# 父窗体在子窗体关闭时打开?
我正在尝试为我将要发布的工具创建登录名,但当我关闭子窗体(登录名)时,主父窗体(工具)将打开。 在我的主工具中,我在C# 父窗体在子窗体关闭时打开?,c#,php,forms,C#,Php,Forms,我正在尝试为我将要发布的工具创建登录名,但当我关闭子窗体(登录名)时,主父窗体(工具)将打开。 在我的主工具中,我在Form1\u Load下有以下内容: Login Log = new Login(); Log.ShowDialog(); 在子表单中,我为这个示例创建了一个简单的登录表单: if (metroTextBox1.Text == "User01") { if (metroTextBox8.Text == "Password") { this.Close()
Form1\u 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();
但当我按下退出按钮时,它会打开主父窗体,允许用户使用它而无需登录,这不是我想要的。实现到窗体1:
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执行:
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 无论登录成功还是失败,您都需要某种方式与父窗体通信。通常的方式是一个属性。在子窗体中,添加属性
public bool Status
{ get; private set; }
然后设置Status=true代码>登录成功且状态=false时代码>否则
您的主表单1\u Load
变为:
Login log = new Login();
log.ShowDialog();
if (!log.Status)
{
Close(); // or whatever else you want to do after a failed login.
}
(请注意,变量名以小写字母开头:log
)无论登录成功还是失败,您都需要某种方式与父窗体通信。通常的方式是一个属性。在子窗体中,添加属性
public bool Status
{ get; private set; }
然后设置Status=true代码>登录成功且状态=false时代码>否则
您的主表单1\u Load
变为:
Login log = new Login();
log.ShowDialog();
if (!log.Status)
{
Close(); // or whatever else you want to do after a failed login.
}
(请注意,变量名以小写开头:log
)