C# 如何关闭表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14381705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:40:17  来源:igfitidea点击:

How to close form

c#winformsbuttonformclosing

提问by osvein

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.

好的,Windows 窗体类 WindowSettings 和窗体有一个“取消”按钮。当用户单击按钮时,对话框 DialogSettingsCancel 将弹出并询问用户是否确定要执行该操作。该对话框有 2 个按钮,一个“是”按钮和一个“否”按钮。如果用户单击“是”按钮,我希望同时关闭 DialogSettingsCancel 和 WindowSettings。

My button_Click event handler in DialogSettingsCancel:

我在 DialogSettingsCancel 中的 button_Click 事件处理程序:

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();
}

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.

当我运行我的应用程序并转到设置表单时,单击“取消”按钮,然后单击“是”按钮,只有 DialogSettingsCancel 关闭而不关闭 WindowSettings。

Why won't it work?

为什么它不起作用?

I've also tried changing

我也试过改变

this.Close();
settings.Close();

to

settings.Close();
this.Close();

But still the same result.

但结果还是一样。

采纳答案by Daniel Hilgarth

You need the actual instance of the WindowSettingsthat's open, not a new one.

您需要打开的实际实例WindowSettings,而不是新实例。

Currently, you are creating a new instance of WindowSettingsand calling Closeon that. That doesn't do anything because that new instance never has been shown.

目前,您正在创建一个新实例WindowSettings并调用Close它。这没有任何作用,因为从未显示过新实例。

Instead, when showing DialogSettingsCancelset the current instance of WindowSettingsas the parent.

相反,当显示DialogSettingsCancel将当前实例设置WindowSettings为父级时。

Something like this:

像这样的东西:

In WindowSettings:

WindowSettings

private void showDialogSettings_Click(object sender, EventArgs e)
{
    var dialogSettingsCancel = new DialogSettingsCancel();
    dialogSettingsCancel.OwningWindowSettings = this;
    dialogSettingsCancel.Show();
}

In DialogSettingsCancel:

DialogSettingsCancel

public WindowSettings OwningWindowSettings { get; set; }

private void button1_Click(object sender, EventArgs e)
{
    this.Close();
    if(OwningWindowSettings != null)
        OwningWindowSettings.Close();
}

This approach takes into account, that a DialogSettingsCancelcould potentially be opened without a WindowsSettingsas parent.

这种方法考虑到 aDialogSettingsCancel可能会在没有WindowsSettings作为父级的情况下打开。

If the two are always connected, you should instead use a constructor parameter:

如果两者始终连接,则应改为使用构造函数参数:

In WindowSettings:

WindowSettings

private void showDialogSettings_Click(object sender, EventArgs e)
{
    var dialogSettingsCancel = new DialogSettingsCancel(this);
    dialogSettingsCancel.Show();
}

In DialogSettingsCancel:

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();
}

回答by SLaks

new WindowSettings();

You just closed a brand new instance of the form that wasn't visible in the first place.

您刚刚关闭了一个全新的表单实例,该实例最初是不可见的。

You need to close the originalinstance of the form by accepting it as a constructor parameter and storing it in a field.

您需要通过接受它作为构造函数参数并将其存储在字段中来关闭表单的原始实例。

回答by Logan G.

Your closing your instance of the settings window right after you create it. You need to display the settings window first then wait for a dialog result. If it comes back as canceled then close the window. For Example:

创建后立即关闭设置窗口的实例。您需要先显示设置窗口,然后等待对话框结果。如果它返回取消,则关闭窗口。例如:

private void button1_Click(object sender, EventArgs e)
{
    Settings newSettingsWindow = new Settings();

    if (newSettingsWindow.ShowDialog() == DialogResult.Cancel)
    {
        newSettingsWindow.Close();
    }
}

回答by Hossein Narimani Rad

send the WindowSettingsas the parameter of the constructor of the DialogSettingsCanceland then on the button1_Clickwhen yes is pressed call the close method of both of them.

发送WindowSettings作为DialogSettingsCancel的构造函数的参数,然后在button1_Click按下 yes 时调用它们的 close 方法。

public class DialogSettingsCancel
{
    WindowSettings parent;

    public DialogSettingsCancel(WindowSettings settings)
    {
        this.parent = settings;        
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //Code to trigger when the "Yes"-button is pressed.
        this.parent.Close();
        this.Close();
    }
}

回答by jforward5

Why not use the DialogResult method to close the form?

为什么不使用 DialogResult 方法来关闭表单?

if(DialogSettingsCancel.ShowDialog() == DialogResult.Yes)
{
     //this will close the form but will keep application open if your 
     //application type is "console" in the properties of the project
     this.Close();
}

For this to work however you will need to do it inside your "WindowSettings" form while you call the DialogSettingsCancel form. Much the same way you would call the OpenFileDialog, or any other Dialog form.

但是,要使其正常工作,您需要在调用 DialogSettingsCancel 表单时在“WindowSettings”表单中执行此操作。与调用 OpenFileDialog 或任何其他 Dialog 窗体的方式非常相似。

回答by jforward5

You can also close the application:

您还可以关闭应用程序:

Application.Exit();

It will end the processes.

它将结束进程。

回答by Jaswanth Chilaka

for example, if you want to close a windows form when an action is performed there are two methods to do it

例如,如果您想在执行操作时关闭窗体,有两种方法可以做到

1.To close it directly

1.直接关闭

Form1 f=new Form1();
f.close(); //u can use below comment also
//this.close();

2.We can also hide form without closing it

2.我们也可以隐藏表单而不关闭它

 private void button1_Click(object sender, EventArgs e)
    {
        Form1 f1 = new Form1();
        Form2 f2 = new Form2();
        int flag = 0;
        string u, p;
        u = textBox1.Text;
        p = textBox2.Text;
        if(u=="username" && p=="pasword")
        {
            flag = 1;
        }
        else
        {
          MessageBox.Show("enter correct details");
        }
        if(flag==1)
        {
            f2.Show();
            this.Hide();
        }

    }

回答by Yuresh Karunanayake

There are different methods to open or close winform. Form.Close() is one method in closing a winform.

打开或关闭 winform 有不同的方法。Form.Close() 是关闭 winform 的一种方法。

When 'Form.Close()' execute , all resources created in that form are destroyed. Resources means control and all its child controls (labels , buttons) , forms etc.

当 'Form.Close()' 执行时,在该表单中创建的所有资源都将被销毁。资源意味着控件及其所有子控件(标签、按钮)、表单等。

Some other methods to close winform

关闭winform的其他一些方法

  1. Form.Hide()
  2. Application.Exit()
  1. 表单.隐藏()
  2. 应用程序.退出()

Some methods to Open/Start a form

打开/启动表单的一些方法

  1. Form.Show()
  2. Form.ShowDialog()
  3. Form.TopMost()
  1. Form.Show()
  2. Form.ShowDialog()
  3. Form.TopMost()

All of them act differently , Explore them !

他们都有不同的行为,探索他们!