C#验证才离开accept_button事件 [英] C# Validate before leaving accept_button event

查看:240
本文介绍了C#验证才离开accept_button事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原谅我,如果这是一个愚蠢的问题,但我是一个beginer这里。

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

我有一个简单的自定义的两个按钮对话框:接受和取消。接受按钮的形式的acceptButton。

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

我想要做一些验证的Accept_Click事件,并决定,如果我能关闭对话框或没有,但每次它离开该方法时,对话框会自动关闭本身并返回OK。

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?

感谢

推荐答案

我想有一个表单级变量(称之为vetoClosing _ )在接受按钮的Click事件,我会根据运行验证和设置变量:

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

然后在的FormClosing事件,我会取消紧密,如果_vetoClosing是真的

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

因为你失去了进入到preSS功能

打开接受按钮关闭是最理想的。

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

这篇关于C#验证才离开accept_button事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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