VB.NET Input Box

Level:
Level1

Sometimes you want to simply get the user's attention and let them know some information. This is easy with the MessageBox. However, what if you not only want to get their attention but you also want them to input some information? In classic VB we would use the InputBox, but where is that in VB.NET. Have no fear this article will show you exactly how to get that same functionality.

An often used feature in classic Visual Basic is the InputBox function. It provides an easy way to prompt the user for simple text input. VB.NET carries this handy function forward for all to use. Although you probably won’t use this any time you need to request a large or amount of data or in other complex scenarios, it does provide a simple and effective way to get input in your Visual Basic.NET apps.

The InputBox Function

Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns a string containing the contents of the text box.

Usage :

stringvariable = InputBox(prompt[, title] [, default] [, xpos] [, ypos])

The arguments to the InputBox function are described below:

Argument

Description

prompt

Required. String expression displayed as the message in the dialog box. The maximum length of prompt is approximately 1024 characters, depending on the width of the characters used.

title

Optional. String expression displayed in the title bar of the dialog box. If you omit title, the application name is placed in the title bar.

default

Optional. String expression displayed in the text box as the default response if no other input is provided. If you omit default, the text box is displayed empty.

xpos and ypos

Both optional. Numeric expressions that specify custom positioning of the box on screen (by default, the box is displayed in the center of the screen, which is usually desired).

Remarks

If the user clicks OK or presses ENTER , the InputBox function returns whatever is in the text box. If the user clicks Cancel, the function returns a zero-length string ("").

Sample Program

The sample program uses a form with two buttons and a label, laid out as shown below:

image

The properties set for these controls are as follows:

(First Button):

  • Name: cmdGetInput
  • Text: Get Input

(Label):

  • Name: lblInfo
  • AutoSize: False
  • BackColor: Khaki
  • BorderStyle: Fixed Single
  • Text: (clear)

(Second Button):

  • Name: cmdExit
  • Text: Exit

The code for the program is shown below:

Public Class Form1 
    PrivateSub cmdGetInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGetInput.Click
        Dim strUserName AsString
 
        strUserName = InputBox("Enter your name:", "InputBox Test", "Type your name here.")
 
        If strUserName = ""Then
            ' user cancelled the InputBox, so clear the label 
            lblInfo.Text = ""
        Else
            lblInfo.Text = "User Name: " & strUserName
        EndIf
    EndSub
 
    PrivateSub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
        Me.Close()
    EndSub
End Class 

When the program is run and you click the "Get Input" button, the following input box is displayed. Note the content of the box based upon the values used as the arguments for the InputBox function:

        strUserName = InputBox("Enter your name:", "InputBox Test", "Type your name here.")

image

Enter your name in the text entry portion of the box (in this example, the name John is entered):

image

After you click OK, the text "User Name: " concatenated with the name you entered will be displayed in the label:

image

You can download the example code for this tutorial here: InputBox VB.NET Example

This article was original written by The VB Programmer.

If you enjoyed this post, subscribe for updates (it's free)