Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Framework  »  Framework 2.0  »  How to Ask the User for Input
 »  Home  »  .NET Framework  »  Framework 3.0  »  How to Ask the User for Input
 »  Home  »  .NET Framework  »  Framework 3.5  »  How to Ask the User for Input
 »  Home  »  .NET Newbie  »  How to Ask the User for Input
 »  Home  »  Visual Studio 2005  »  How to Ask the User for Input
 »  Home  »  Visual Studio 2008  »  How to Ask the User for Input
 »  Home  »  Windows Development  »  How to Ask the User for Input
How to Ask the User for Input
by Larry Blake | Published  10/27/2009 | Framework 2.0 Framework 3.0 Framework 3.5 .NET Newbie Visual Studio 2005 Visual Studio 2008 Windows Development | Rating:
Larry Blake
Larry has been a professional developer since the 1980s, on a broad range of platforms. An early adopter of PC technology, he started fooling around with Visual Basic 1.0 in 1992. Today he is Microsoft MVP in Visual Basic. Larry is available for contract coding in VB, C# and ASP, and for Project Management. See: . 

View all articles by Larry Blake...
How to Ask for Input


The simplest way of asking questions is to use a MessageBox, and give the user a choice of buttons to press.  You can use the optional third parameter to specify what buttons are shown, then test the result.  For example:

Dim res As DialogResult
res = MessageBox.Show("Are you sure you want to delete this record?", "Confirm", MessageBoxButtons.YesNo)
If (res = DialogResult.Yes) Then
' Perform deletion
End If

As you develop this code, IntelliSense will suggest good values for the button combinations (MessageBoxButtons and DialogResult).  The nice thing about this technique is that it's simple, and the user can't enter anything unexpected. Compare this to:


If you use an InputBox like the one on the right, you might need some additional error checking.  For example, if you expect an Integer, but the user types in "3.14159" or "I don't have one", your program could abend if you don't handle the conversion.  InputBox code looks like this:

Dim fav As String = "6"
fav = InputBox("What's your favorite number?", "Favorite Number", fav)
Dim n As Integer
Dim ok As Boolean = Integer.TryParse(fav, n)
If (ok) Then
' Continue
End If

To insure a valid entry, you could wrap the InputBox code in a loop, redisplaying it each time something invalid is entered (possibly with a MessageBox explaining why).  As a user, I would find this distracting.

Another disadvantage is that the "Cancel" button doesn't do anything.  The programmer needs to test for nothing entered, or similar, to recognize that the user has given up.  So the prompt should give a hint how to do this.  In my example, I gave a starting value, which would cause "Cancel" to execute the "Continue" code using the number 6.

Custom Form

Sometimes the answer is a simple custom form. If you want to restrict input to a range of integers, showing a form with a ComboBox is an error-proof interface, and it's a bit more professional-looking. Design the form as usual, and add OK and Cancel buttons. Then set the following form properties: FormBorderStyle = FixedDialog , AcceptButton = {your OK button name } , CancelButton = {your Cancel button name }.  AcceptButton assigns the named button to execute when Enter is pressed.  CancelButton does the same for Esc.  In my form, I have also set ControlBox = False , which removes the Min / Max / Close buttons.

Some more properties.  On the "OK" button, set DialogResult = OK .  On "Cancel", set DialogResult = Cancel .  As in MessageBox, you can now test for which one was pressed. There is actually no need for additional code in the button events.  With the custom form named "FormPicker", the code in the calling form looks like this:

Dim fav As Integer = 6
Dim fp As New FormPicker(fav)
If (fp.ShowDialog() = DialogResult.OK) Then
   Dim n As Integer = fp.Level
   ' Continue
End If

For this to work, we need a little more code on the custom form, but pretty simple, right?  Here's all we need to do.

The form needs a Public Property, Level , so that the calling form can retrieve the value.  And it needs a Constructor to accept the initial value.  (See my article on custom objects if you don't know about Constructors and Public Properties.) For the property:

Private _level As Integer
Public ReadOnly Property Level() As Integer
   End Get
End Property

For the Constructor:

Public Sub New(ByVal lev As Integer)
   _level = lev

End Sub

To position to the selected value:

Private Sub FormPicker_Load(...
ComboBox1.SelectedItem = _level.ToString()
End Sub

And to set the value when the combobox changes:

Private Sub ComboBox1_SelectedIndexChanged(...
   _level = CType(ComboBox1.SelectedItem, Integer)
End Sub

A little bit more complicated, and a lot more flexible.

I hope this helped you.

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:1.64788732394365 out of 5
 355 people have rated this page
Article Score8200
Sponsored Links