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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With