如何关闭表格 [英] How to close form
问题描述
好,因此Windows窗体类,WindowSettings和窗体具有取消"按钮.当用户单击按钮时,将弹出对话框DialogSettingsCancel,询问用户是否确定要执行该操作.该对话框有2个按钮,是"按钮和否"按钮.如果用户单击是"按钮,则我希望同时关闭DialogSettingsCancel和WindowSettings.
Ok, so a Windows Forms class, WindowSettings, and the form has a "Cancel"-button. When the user clicks the button, the dialog DialogSettingsCancel will pop-up up and ask the user if he is sure he wants to perform the action. The dialog has 2 buttons, a "Yes"-button and a "No"-button. If the user clicks the "Yes"-button, I want both DialogSettingsCancel and WindowSettings to be closed.
我在DialogSettingsCancel中的button_Click事件处理程序:
My button_Click event handler in DialogSettingsCancel:
private void button1_Click(object sender, EventArgs e)
{
//Code to trigger when the "Yes"-button is pressed.
WindowSettings settings = new WindowSettings();
this.Close();
settings.Close();
}
运行应用程序时,转到设置表单,单击取消"按钮,然后单击是"按钮,只有DialogSettingsCancel关闭而不关闭WindowSettings.
When I run my application, and go to the settings form, and click the "Cancel"-button, and then click the "Yes"-button, only DialogSettingsCancel closes without closing WindowSettings.
为什么不起作用?
我也尝试过更改
this.Close();
settings.Close();
到
settings.Close();
this.Close();
但是结果还是一样.
推荐答案
您需要打开的WindowSettings
的实际实例,而不是新实例.
You need the actual instance of the WindowSettings
that's open, not a new one.
当前,您正在创建WindowSettings
的新实例,并在其上调用Close
.这没有任何作用,因为从未显示过该新实例.
Currently, you are creating a new instance of WindowSettings
and calling Close
on that. That doesn't do anything because that new instance never has been shown.
相反,在显示DialogSettingsCancel
时,将WindowSettings
的当前实例设置为父级.
Instead, when showing DialogSettingsCancel
set the current instance of WindowSettings
as the parent.
类似这样的东西:
在WindowSettings
中:
private void showDialogSettings_Click(object sender, EventArgs e)
{
var dialogSettingsCancel = new DialogSettingsCancel();
dialogSettingsCancel.OwningWindowSettings = this;
dialogSettingsCancel.Show();
}
在DialogSettingsCancel
中:
public WindowSettings OwningWindowSettings { get; set; }
private void button1_Click(object sender, EventArgs e)
{
this.Close();
if(OwningWindowSettings != null)
OwningWindowSettings.Close();
}
此方法考虑了DialogSettingsCancel
可能在没有WindowsSettings
作为父项的情况下打开的可能性.
This approach takes into account, that a DialogSettingsCancel
could potentially be opened without a WindowsSettings
as parent.
如果两者始终连接,则应改用构造函数参数:
If the two are always connected, you should instead use a constructor parameter:
在WindowSettings
中:
private void showDialogSettings_Click(object sender, EventArgs e)
{
var dialogSettingsCancel = new DialogSettingsCancel(this);
dialogSettingsCancel.Show();
}
在DialogSettingsCancel
中:
WindowSettings _owningWindowSettings;
public DialogSettingsCancel(WindowSettings owningWindowSettings)
{
if(owningWindowSettings == null)
throw new ArgumentNullException("owningWindowSettings");
_owningWindowSettings = owningWindowSettings;
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
_owningWindowSettings.Close();
}
这篇关于如何关闭表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!