Input Boxes |
|
Introduction |
The Visual Basic language provides a function that allows you to request information from the user who can type it in a text field of a dialog box:
Creating an Input Box |
The function used to accomplish this is called InputBox and its basic syntax is:
Public Function InputBox( _ ByVal Prompt As String, _ Optional ByVal Title As String = "", _ Optional ByVal DefaultResponse As String = "", _ Optional ByVal Xpos As Integer = -1, _ Optional ByVal YPos As Integer = -1 _ ) As String
The Message of an Input Box |
The most basic piece of information you can provide to the InputBox() function is referred to as the prompt. It should be a string that the user will read and know what you are expecting. Here is an example:
Private Sub cmdInputBox_Click() InputBox "Enter your date of birth:" End Sub
This would produce
Upon reading the message on the input box, the user is asked to enter a piece of information. The type of information the user is supposed to provide depends on you, the programmer. Therefore, there are two important things you should always do. First you should let the user know what type of information is requested. Is it a number (what type of number)? Is it a string (such as the name of a country or a customer's name)? Is it the location of a file (such as C:\Program Files\Homework)? Are you expecting a Yes/No True/False type of answer (if so, how should the user provide it)? Is it a date (if it is a date, what format is the user supposed to enter)? These questions mean that you should state a clear request to the user and specify what kind of value you are expecting. For example, instead of the question above, you can implement the InputBox() function as follows:
Public Function Main() As Integer InputBox("Please enter your date of birth as mm/dd/yyyy") Return 0 End Function
This would produce:
Another solution, also explicit enough, consists of providing an example to the user.
The Title of an Input Box |
The second argument to the InputBox() function allows you to optionally specify the title of the input box. This is the string that would appear on the title bar. Since this is an optional argument, if you don't pass it, the input box would display the name of the application. Otherwise, to display your own title bar, pass the Title argument.
The title is passed as a string. Here is an example:
Private Sub cmdInputBox_Click() InputBox "Enter your date of birth as mm/dd/yyyy", _ "Student Registration" End Sub
This would produce:
Notice that the caption is now customized instead of the name of the application. The caption can also be a string created from an expression or emanating from a variable or value.
The Default Value of an Input Box |
Sometimes, even if you provide an explicit request, the user might not provide a new value but click OK. The problem is that you would still need to get the value of the text box and you might want to involve it in an expression. You can solve this problem and that of providing an example to the user by filling the text box with a default value. To support this, the InputBox() function provides the third argument.
To present an example or default value to the user, pass a third argument to the InputBox() function. If you want to use this argument to provide an example the user can follow, provide it with the right format. Here is an example:
Private Sub cmdInputBox_Click() InputBox "Enter Student Name:", _ "Student Registration", "John Doe" End Sub
Here is an example of running the program:
Notice that, when the input box displays with a default value, the value is in the text box and the value is selected. Therefore, if the value is fine, the user can accept it and click OK. Another way you can use the default value is to provide a value the user can accept; that is, the most common or most likely value the user would enter. Here is an example:
Private Sub cmdInputBox_Click() InputBox "Enter Birth State:", _ "Student Registration", "VA" End Sub
Here is an example of running the program:
Once again, notice that the user can just accept the value and click OK or press Enter.
The Location of the Input Box |
By default, when the input box comes up, it displays in the middle of the screen. If you want, you can specify where the input box should be positioned when it comes up. To assist you with this, the InputBox() function is equipped with a fourth and a fifth arguments. The fourth argument specifies the x coordinate of the input box; that is, the distance from its left border to the left border of the monitor. The fifth argument specifies the distance from the top border of the input box to the top border of the monitor.
The Return Value of an Input Box |
When the input box displays, after typing a value, the user would click one of the buttons: OK or Cancel. If the user clicks OK, you should retrieve the value the user would have typed. It is also your responsibility to find out whether the user typed a valid value. Because the InputBox() function can return any type of value, it has no mechanism of validating the user's entry. To retrieve the value of the input box dialog when the user clicks OK, you can get the returned value of the InputBox() function.
After being used, the InputBox() function returns a string. Here is an example of getting it:
Private Sub cmdInputBox_Click() Dim StudentName As String StudentName = InputBox("Enter Student Name:", _ "Student Registration") MsgBox "Student Name: " & StudentName End Sub
You can also get any type of value from an input box. That is, when the InputBox() function exits, thanks to the flexibility of the Visual Basic language, the compiler can directly cast the returned value for you. Here is an example:
Private Sub cmdInputBox_Click() Dim DateOfBirth As Date DateOfBirth = InputBox("Please enter your date of birth as mm/dd/yyyy", _ "Student Registration") MsgBox "Date of Birth: " & DateOfBirth End Sub
|
||
Home | Copyright © 2009 FunctionX | |
|