简体   繁体   中英

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.

I am using bindingNavigator to update records in a table of database. 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). 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 :

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; I do this in the form load.

this alone still won't do it. you also need to set the focus. bindingnavigator.focus(); I do this in the bindingnavigator_ItemClicked event so it happens no matter what button is clicked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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