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: Public Function Main() As Integer InputBox("Please enter your date of birth as mm/dd/yyyy", _ "Student Registration") Return 0 End Function 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.
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: Public Function Main() As Integer InputBox("Enter Student Name:", _ "Student Registration", "John Doe") Return 0 End Function 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: Public Function Main() As Integer InputBox("Enter Birth State:", _ "Student Registration", "VA") Return 0 End Function 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.
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.
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: Public Function Main() As Integer Dim StudentName As String StudentName = InputBox("Enter Student Name:", _ "Student Registration") MsgBox("Student Name: " & StudentName) Return 0 End Function 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: Public Function Main() As Integer Dim DateOfBirth As Date DateOfBirth = InputBox("Please enter your date of birth as mm/dd/yyyy", _ "Student Registration") MsgBox("Date of Birth: " & DateOfBirth) Return 0 End Function |
|
|||||||||
|