control validating event and terminating

phanks

New member
Joined
Mar 25, 2009
Messages
4
Programming Experience
1-3
I have a form with a combobox that I added the validating event to verify the user has input a value. If the user does not select a value, I set e.cancel to true returning them to input a value.

If ((cboName.SelectedIndex = -1) And (cboName.Text <> "")) Then
If (MsgBox("'" & cboName.Text & "' was not found, Do you want to add it?", _
MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Not Found") = vbYes) Then
' Handle the add to database
Else
e.Cancel = True
End If
Else
If (cboName.SelectedIndex = 0) Then
e.Cancel = True
cboName.SelectedIndex = -1
End If

The problem I have now is I am unable to terminate the app until they enter something. What is the best way to validate the input selection but still be able to abort the form?
 
One way to do this is:

If you don't already have one, add a Cancel Button to your Form. Steps for this (sorry if you already know this, just in case)
1) Add Button to Form
2) Set Text property of Button to 'Cancel' (or 'Abort', or whatever suits)
3) Set DialogResult property of Button to Cancel
4) Set CancelButton property of Form to the Button from above

Then, so that you can bomb out without the validation
5) Set CausesValidation property for your Cancel Button to False.

This should work. :)
 
Back
Top