C# 单击确定按钮时防止 ShowDialog() 返回

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10330882/
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-09 13:25:54  来源:igfitidea点击:

Prevent ShowDialog() from returning when OK button is clicked

c#winformsdialogshowdialog

提问by user1151923

I have a dialog that I want to prevent from closing when the OK button is clicked, but it returns, and that even if the AcceptButton property is set to none instead of my OK button. What is the best way to stop it from closing?

我有一个对话框,我想在单击 OK 按钮时阻止它关闭,但它会返回,即使 AcceptButton 属性设置为 none 而不是我的 OK 按钮。阻止它关闭的最佳方法是什么?

采纳答案by David Heffernan

In fact you are changing the wrong property. You certainly do want AcceptButtonto be the OK button. This property determines which is the defaultbutton in Windows terms. That is the button which is pressed when you hit ENTERon your keyboard. By changing AcceptButtonyou are simply breaking the keyboard interface to your dialog. You are not influencing in any way what happens when the button is pressed.

事实上,您正在更改错误的属性。您当然希望AcceptButton成为“确定”按钮。此属性确定哪个是Windows 术语中的默认按钮。这是您敲击ENTER键盘时按下的按钮。通过更改,AcceptButton您只是将键盘界面破坏到您的对话框。您不会以任何方式影响按下按钮时发生的情况。

What you need to do is set the DialogResultproperty of your button to DialogResult.Nonesince that's what determines whether or not a button press closes the form. Then, inside the button's click handler you need to decide how to respond to the button press. I expect that, if the validation of the dialog is successful, you should close the dialog by setting the form's DialogResultproperty. For example

您需要做的是将DialogResult按钮的属性设置为,DialogResult.None因为这决定了按下按钮是否关闭表单。然后,在按钮的单击处理程序中,您需要决定如何响应按钮按下。我希望,如果对话框验证成功,您应该通过设置表单的DialogResult属性来关闭对话框。例如

private void OKbuttonClick(object sender, EventArgs e)
{
    if (this.CanClose())
        this.DialogResult = DialogResult.OK;
}

回答by Adam Houldsworth

You need to remove the DialogResultof the button itself as well, in the properties window on the button set it to None.

您还需要删除DialogResult按钮本身的,在按钮的属性窗口中将其设置为None

http://msdn.microsoft.com/en-us/library/system.windows.forms.button.dialogresult.aspx

http://msdn.microsoft.com/en-us/library/system.windows.forms.button.dialogresult.aspx

If the DialogResult for this property is set to anything other than None, and if the parent form was displayed through the ShowDialog method, clicking the button closes the parent form without your having to hook up any events.

如果此属性的 DialogResult 设置为 None 以外的任何值,并且父窗体是通过 ShowDialog 方法显示的,则单击该按钮将关闭父窗体,而无需连接任何事件。

Obviously, now your button won't do anything so you will need to register a handler for the Clickevent.

显然,现在您的按钮不会执行任何操作,因此您需要为该Click事件注册一个处理程序。

回答by Steve

The best way to stop this behavior is changing the DialogResult property of your OK button to DialogResult.Nonein the property window at design time.

停止此行为的最佳方法是DialogResult.None在设计时将 OK 按钮的 DialogResult 属性更改为在属性窗口中。

Also, If you have already some code in the click event of the OK button you could change the form DialogResult.

此外,如果您在 OK 按钮的单击事件中已经有一些代码,您可以更改表单 DialogResult。

private void comOK_Click(object sender, EventArgs e)
{
    // your code .....

    // Usually this kind of processing is the consequence of some validation check that failed
    // so probably you want something like this
    if(MyValidationCheck() == false)
    {
        // show a message to the user and then stop the form closing with
        this.DialogResult = DialogResult.None;
    }
}

回答by Raheel Khan

The best practice is to actually set the Ok button to be disabled rather than not respond to user input.

最佳实践是将 Ok 按钮实际设置为禁用,而不是不响应用户输入。

The DialogResult property SHOULD be set to Ok or Yes depending on the form and the AcceptButton should also be linked to Ok.

DialogResult 属性应该根据表单设置为 Ok 或 Yes,并且 AcceptButton 也应该链接到 Ok。

I normally create a function on all dialogs and call it whenever the user interacts with the data.

我通常在所有对话框上创建一个函数,并在用户与数据交互时调用它。

void RefreshControls() { button.Enabled = this.ValidateInput(); }

void RefreshControls() { button.Enabled = this.ValidateInput(); }

回答by Wasyster

 static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Form2 fLogin = new Form2();
            if (fLogin.ShowDialog() == DialogResult.OK)
            {
                Application.Run(new Form1());
            }
            else
            {
                Application.Exit();
            }
        }
    }

public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void btnKlik_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
    }

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }