Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I "Cancel an event"?

I have two DateTimePickers dtpDateFrom and dtpDateTo.

I already coded a conditional statement that if dtpDateTo is earlier than dtpDateFrom via their ValueChanged event, I would get an error message, but it still applies the change.

Example:

  1. dtpDateFrom = July 2
  2. dtpDateTo = July 3
  3. If I change dtpDateFrom to July 4 ~> error message + without performing the July 4 change
like image 492
user3386068 Avatar asked Oct 21 '25 19:10

user3386068


1 Answers

Try to use the Validating event for the control. In your code where your validation fails put in e.cancel = true

Private Sub dtpDateFrom_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles dtpDateFrom.Validating
    If dtpDateFrom.Value > dtpDateTo.Value Then
        Messagebox.show("From date must be less than To date")
        e.Cancel = True 'Validation failed.
    End If
End Sub
like image 143
ShawnOrr Avatar answered Oct 24 '25 03:10

ShawnOrr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!