简体   繁体   English

在C#中,如何在使用bindingNavigator时取消默认的鼠标单击事件

[英]In C#, How to cancel the default mouse click event when using bindingNavigator

I am a new C# user and now I have encountered a problem in using BindingNavigator. 我是一个新的C#用户,现在我遇到了使用BindingNavigator的问题。

I am using bindingNavigator to update records in a table of database. 我使用bindingNavigator来更新数据库表中的记录。 Before I leave current updated record and enter the next record by clicking Next button, I will perform some validation, if there is any thing incorrect, I hope it could raise a warning to give me chance to correct the wrong fields instead of moving to the next record. 在我离开当前更新的记录并通过单击下一步按钮输入下一条记录之前,我将执行一些验证,如果有任何不正确的事情,我希望它可以发出警告,让我有机会纠正错误的字段而不是转移到下一个记录。

I added some lines in bindingNavigatorMoveNextItem_MouseDown event, but it still move to next item even there are some thing wrong with current record(fields have some logical connection). 我在bindingNavigatorMoveNextItem_MouseDown事件中添加了一些行,但它仍然移动到下一个项目,即使当前记录有一些问题(字段有一些逻辑连接)。 Can any expert here help me out about that? 这里有专家可以帮我解决这个问题吗? Many Thanks! 非常感谢!

You have two approaches: either overriding WndProc and prevent mouse click window message from calling the base's WndProc , or simply overriding OnMouseClick : 您有两种方法:覆盖WndProc并阻止鼠标单击窗口消息调用基础的WndProc ,或者只是覆盖OnMouseClick

class Hello : BindingNavigator
{
    private bool canFire = false;
    protected override void OnMouseClick(MouseEventArgs e) // second approach
    {
        // don't call base method so that event doesn't fire up
        if (this.canFire)
             base.OnMouseClick(e);
    }
}

I know this is old.. but for anyone else... You should use the normal buttons, and just use the validating event, canceling if anything fails your validation. 我知道这是旧的......但是对于其他任何人......你应该使用普通按钮,只使用验证事件,如果验证失败则取消。 The control does not display the property in the designer, but you can still set it : bindingnavigator.CausesValidation = true; 控件不会在设计器中显示属性,但您仍可以设置它:bindingnavigator.CausesValidation = true; I do this in the form load. 我在表单加载中执行此操作。

this alone still won't do it. 仅此一点仍然不会这样做。 you also need to set the focus. 你还需要设置焦点。 bindingnavigator.focus(); bindingnavigator.focus(); I do this in the bindingnavigator_ItemClicked event so it happens no matter what button is clicked. 我在bindingnavigator_ItemClicked事件中执行此操作,因此无论点击什么按钮都会发生这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM