tags:

views:

372

answers:

4

I have a simple windows application that pops up an input box for users to enter in a date to do searches.

How do I identify if the user clicked on the Cancel button, or merely pressed OK without entering any data as both appear to return the same value?

I have found some examples of handling this in VB 6 but none of them really function in the .NET world.

Ideally I would like to know how to handle the empty OK and the Cancel seperately, but I would be totally ok with just a good way to handle the cancel.

+1  A: 
input = InputBox("Text:")

If input <> "" Then
   ' Normal
Else
   ' Cancelled, or empty
End If

From MSDN:

If the user clicks Cancel, the function returns a zero-length string ("").

Kyle Rozendo
This would work, but is there a way to tell the difference between a cancel button push or an empty ok? (Like what Jamie posted below, but in a way that works with .NET)
The Sasquatch
The input box is very limited, as Jamie says, just write your own dialog instead.
ho1
A: 
Dim input As String

input = InputBox("Enter something:")

If StrPtr(input) = 0 Then
   MsgBox "You pressed cancel!"
Elseif input.Length = 0 Then
   MsgBox "OK pressed but nothing entered."
Else
   MsgBox "OK pressed: value= " & input
End If
Jamie
This is what I am looking for conceptually, but StrPtr isn't valid in VB.NET
The Sasquatch
A: 

You could just create your own simple InputBox form with just a textbox and a couple of buttons. Here is a quick example...

http://www.dreamincode.net/forums/topic/103846-custom-inputbox/

Jamie
A: 

Here is what I did and it worked perfectly for what I was looking to do:

Dim StatusDate As String
 StatusDate = InputBox("What status date do you want to pull?", "Enter Status Date", " ")

        If StatusDate = " " Then
            MessageBox.Show("You must enter a Status date to continue.")
            Exit Sub
        ElseIf StatusDate = "" Then
            Exit Sub
        End If

This key was to set the default value of the input box to be an actual space, so a user pressing just the OK button would return a value of " " while pressing cancel returns ""

From a usability standpoint, the defaulted value in the input box starts highlighted and is cleared when a user types so the experience is no different than if the box had no value.

The Sasquatch