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:
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
Private Sub cmdGetInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGetInput.Click
Dim strUserName As String
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
End If
End Sub
Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
Me.Close()
End Sub
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.")
Enter your name in the text entry portion of the box (in this example, the name John is entered):
After you click OK, the text "User Name: " concatenated with the name you entered will be displayed in the label:
Download the VB project files for the example above here.