Sunday, March 4, 2012

VB.NET Input Box

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.

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:

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.

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.

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.

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: cmdGetInputText: Get Input

(Label):

Name: lblInfoAutoSize: FalseBackColor: KhakiBorderStyle: Fixed SingleText: (clear)

(Second Button):

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() EndSubEnd 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.


View the original article here

No comments:

Post a Comment