关闭表单时取消自定义UserControl上的验证事件 [英] Cancel validation event on custom UserControl when form closing

查看:75
本文介绍了关闭表单时取消自定义UserControl上的验证事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义控件,该控件在加载表单时会获得焦点.它使用验证事件来处理它为空或输入无效数据的情况.

I have a custom control that gets focus when the form is loaded. It uses validating event on it that handles it being left empty or having invalid data entered.

我的问题是,当用户加载表单,然后将其关闭时,它正在进行验证过程并显示错误,因为它为空.

My problem is, when the user loads the form, then just closes it, it is going through the validation process and displaying errors because it is left empty.

在BaseForm(:Form)上,我们已经覆盖了WndProc方法,并在表单上设置了一个标志来表示它正在关闭,现在在验证事件处理程序中,我可以获取父表单,并取消表单的返回正在关闭.

On the BaseForm (: Form) we have overwritten the WndProc method and set a flag on the form to say it is closing, now in the validating event handler I can get the parent form and cancel return from the method if the form is closing.

我要执行的操作是在对象上执行此检查,以便它影响所有现有实例,并在关闭窗体时取消验证事件.我只是无法取消活动.

What I am wanting to do it perform this check on the object so it affected all of the existing instances and cancels the validating events when the form is closing. I just cannot get the event to cancel.

这是UserControl代码的外壳.

Here is a shell of the UserControl Code.

public class SearchControl : UserControl
{
    public SearchControl()
    {
        Validating += OnControlValitading;
    }

    public void OnControlValitading(object sender, CancelEventArgs e)
    {
        BaseForm  frm = FindForm() as BaseForm;
        if(frm != null && frm.IsClosing)
        {
            e.Cancel = true;
            //TODO Cancel validation event on all objects
        }
    }
}

推荐答案

覆盖OnValidating并使用CancelEventArgs:

Override OnValidating and use the CancelEventArgs:

public class SearchControl : UserControl
{

    protected override void OnValidating(CancelEventArgs e)
    {
        BaseForm frm = FindForm() as BaseForm;
        if (frm != null && frm.IsClosing)
        {
            e.Cancel = true;
            //TODO Cancel validation event on all objects
        }
    }
}

这篇关于关闭表单时取消自定义UserControl上的验证事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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