C# 在离开accept_button事件之前进行验证

发布于 2024-07-17 10:38:16 字数 232 浏览 2 评论 0原文

如果这是一个愚蠢的问题,请原谅,但我是这里的初学者。

我有一个简单的自定义对话框,带有两个按钮:接受和取消。 Accept按钮是表单的acceptButton。

我想对 Accept_Click 事件进行一些验证,并决定是否可以关闭对话框,但每次离开此方法时,对话框都会自动关闭并返回 Ok。

我该怎么做才能阻止对话框自行关闭? 或者我必须以其他方式做事?

谢谢

Excuse me if this is a silly question but i'm a beginer here.

I have a simply custom dialog with two buttons: Accept and Cancel. The Accept button is the acceptButton of the form.

I want to do some validations on the Accept_Click event and decide if i can close the dialog or not, but everytime it leaves this method, the dialog automatically closes itself and returns Ok.

What can I do to stop the dialog from closing itself? or i have to do things in some other way?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

七颜 2024-07-24 10:38:16

我将有一个表单级别变量(称之为 _vetoClosing) 在接受按钮的 Click 事件中,我将运行验证并根据该变量设置变量:

    private void acceptButton_Click(object sender, EventArgs e)
    {
        // Am I valid
        _vetoClosing = !isValid();
    }

然后在 FormClosing 事件中,我将取消关闭,如果_vetoClosing 为 true

    private void Form_FormClosing(object sender, FormClosingEventArgs e)
    {
        // Am I allowed to close
        if (_vetoClosing)
        {
            _vetoClosing = false;
            e.Cancel = true;
        }
    }

关闭“接受”按钮不是最佳选择,因为您会失去 Enter to Press 功能。

I would have a form level variable (call it _vetoClosing) In the accept button's Click event, I would run validation and set the variable based on that:

    private void acceptButton_Click(object sender, EventArgs e)
    {
        // Am I valid
        _vetoClosing = !isValid();
    }

Then in the FormClosing event, I would cancel close if _vetoClosing is true

    private void Form_FormClosing(object sender, FormClosingEventArgs e)
    {
        // Am I allowed to close
        if (_vetoClosing)
        {
            _vetoClosing = false;
            e.Cancel = true;
        }
    }

Turning Accept button off is suboptimal because you loose the Enter to Press functionality.

时光瘦了 2024-07-24 10:38:16

更简洁的解决方案是将 DialogResult 设置为 None:

private void acceptButton_Click(object sender, EventArgs e)
{
    if (!isValid()) {
        this.DialogResult = System.Windows.Forms.DialogResult.None;
    }
}

A cleaner solution would be to set DialogResult to None:

private void acceptButton_Click(object sender, EventArgs e)
{
    if (!isValid()) {
        this.DialogResult = System.Windows.Forms.DialogResult.None;
    }
}
王权女流氓 2024-07-24 10:38:16

我将在控件更改时进行验证,并且仅在整个表单有效时才启用“接受”按钮。

这将允许您将按钮保留为默认按钮 (AcceptButton),但可以防止这种情况发生。

I would validate as the controls change, and only enable the Accept button if the whole form is valid.

This would allow you to keep your button as the default button (AcceptButton), but prevent this from occurring.

演多会厌 2024-07-24 10:38:16

表单上的 AcceptButton 或 CancelButton 是否设置为该按钮? 如果是这样,请尝试取消设置并在想要关闭对话框时在处理程序中手动设置 DialogResult。

Is the AcceptButton or CancelButton on the form set to that button? If so, try unsetting it and manually setting DialogResult in your handler when you want to close the dialog.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文