Visual Basic Functions: The Input Box |
|
Introduction |
An input box is a specially designed dialog box that allows the programmer to request a value from the user and use that value as necessary. An input box displays a title, a message to indicate the requested value, a text box for the user, and two buttons: OK and Cancel. Here is an example: |
When an input box displays, it presents a request to the user who can then provide a value. After using the input box, the user can change his or her mind and press Esc or click Cancel. If the user provided a value and want to acknowledge it, he or she can click OK or press Enter. This would transfer the contents of the text box to the application that displayed the input box.
To support input boxes, the Visual Basic library provides a function named InputBox. Its 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 only required argument of this function is the message that prompts the user. Here is an example: Private Sub btnMessage_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnMessage.Click InputBox("Enter Student's Date of Birth") End Sub This would produce
When calling the InputBox() function, if you pass only the first argument, the input box would display the name of the application in the title bar. If you want, you can specify your own caption through the Title argument. Here is an example: Private Sub btnMessage_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnMessage.Click InputBox("Enter Student's Date of Birth", _ "Red Oak High School - Student Registration") End Sub This would produce
When 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 the type of information 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 indicate that you should state a clear request to the user. To assist the user with the type of value you are expecting, you can give an example or a type. To support this, the InputBox() function is equipped with the third argument as a string. When passing it, you can provide a sample value that the user would follow. Here is an example: Private Sub btnMessage_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnMessage.Click InputBox("Enter Student's Date of Birth", _ "Red Oak High School - Student Registration", _ "MM/DD/YYYY") End Sub This would produce: The last two arguments, XPos and YPos, allow you to specify the default position of the input box when it comes up the first time. After typing a value, the user would click one of the buttons: OK or Cancel. If the user clicks OK, you can retrieve the value the user would have typed. It is also your responsibility to find out whether the user typed a valid value or not. Because the InputBox() function returns a string, it has no mechanism of validating the user's entry. Therefore, if necessary, you must convert the return value of the input box when the user clicks OK. Here is an example: Private Sub btnMessage_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnMessage.Click Dim strDOB As String strDOB = InputBox("Enter Student's Date of Birth", _ "Red Oak High School - Student Registration", _ "MM/DD/YYYY") If IsDate(strDOB) Then MsgBox("The student was born on " & CDate(strDOB).ToString("D"), _ MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, _ "Red Oak High School - Student Registration") Else MsgBox("You provided an invalid value", _ MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, _ "Red Oak High School - Student Registration") End If End Sub
|
|
||
Home | Copyright © 2008-2016, FunctionX, Inc. | Home |
|