如何关闭表格 [英] How to close form

查看:159
本文介绍了如何关闭表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,因此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屋!

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