BUG: Setting CancelEventArgs to True is failing

We are handling the Validating event for a control.  If a conditional test fails in this event handler, we set CancelEventArgs = True and call SetError on ErrorProvider :

If txtBox.Text.Trim = String.Empty Then
                Me.ReviewErrorProvider.SetError(DirectCast(sender,
txtBox), "Required Field")
                e.Cancel = True

There is a button on the same form as the control.  Call this buttonA.  When clicking on buttonA after setting CancelEventArgs = True in txtBox's validating handler, the Click Event fires for buttonA.  Since we called e.Cancel = True, this event should not fire.

Now, if we add a breakpoint on the call to CancelEventArgs = True in txtBox's validating handler, buttonA's click event is blocked (the event does not fire).

So, what is it about the breakpoint that makes this work? 

This has been reported as a bug to Microsoft. 

 

2 Comments

  • Are you saying that after running past the breakpoint, the click event still doesn't fire? I can't explain that, but I disagree with the assertion that the Click event should not fire if you set e.Cancel = true in the Validating event. You are canceling the Validatiing event, not every event that led up to that event firing.

  • With the breakpoint in the validating event for the textbox, the click event is canceled for the button; at least it does not fire as we have another break point in the click event handler. Without the break point in either, the click event does fire and code in the handler runs.

Comments have been disabled for this content.