+ Reply to Thread
Results 1 to 3 of 3

Thread: control validating event and terminating

  1. #1
    phanks is offline VB.NET Forum Newbie phanks is on a distinguished programming path ahead
    .NET Framework
    .NET 3.0 (VS 2005/2008)
    Join Date
    Mar 2009
    Posts
    4
    Reputation
    0

    Default control validating event and terminating

    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?

  2. #2
    henryminute is offline VB.NET Forum Enthusiast henryminute is on a distinguished programming path ahead
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Jul 2009
    Location
    UK
    Posts
    57
    Reputation
    16

    Default

    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.
    Some create happiness wherever they go, others whenever - Oscar Wilde

  3. #3
    phanks is offline VB.NET Forum Newbie phanks is on a distinguished programming path ahead
    .NET Framework
    .NET 3.0 (VS 2005/2008)
    Join Date
    Mar 2009
    Posts
    4
    Reputation
    0

    Lightbulb

    Thanks I was missing step 4. That did the trick.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts