Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET Inputbox - How to identify when the Cancel Button is pressed?

Tags:

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.

like image 677
The Sasquatch Avatar asked May 27 '10 18:05

The Sasquatch


People also ask

How to handle Cancel on a InputBox?

The easiest and most effective way that I have found to handle the siutation where a user clicks the InputBox Function's Cancel button is to use the StrPtr Function to identify when the Cancel button has been clicked.

What is an input box in Visual Basic?

VBA InputBox is used to prompt the user to enter the values. This message box is used to displaying a message and waits for the user action performed by pressing the button. A text can be return in the text box by using the InputBox function if the user clicks on the OK or Enter button.


1 Answers

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.

like image 61
The Sasquatch Avatar answered Oct 16 '22 00:10

The Sasquatch