Download presentation
Presentation is loading. Please wait.
1
Messages and Input boxes
Visual Basic 12/30/2018 Prepared By: Deborah Becker
2
The highlights of this Lecture
How message boxes differ from text boxes Why functions benefit programmers When to test message box return values Why to add remarks How to receive input box answers In your application will need to display messages and ask questions of the user. The application needs to receive the user's response from the questions. Although the Label and Text Box controls work well for giving and receiving user information, such controls don't lend themselves to messages and questions that the program displays during execution such as error messages and warning boxes. For example, suppose you want to know if the user has prepared the printer for printing. To prepare a printer, the user has to turn on the printer, make sure paper is there, and ensure that the online light is on. Your program should not attempt to print a report until the user has performed these actions or an error will occur. Therefore, when the user initiates a report for printing, your application can gather the data and then ask the user if the printer is ready. If the user responds affirmatively, you can start the report's output. The form's controls simply do not provide such interaction. The highlights of this hour include 12/30/2018 Prepared By: Deborah Becker
3
Msgbox and Inputbox Functions
Remember –The function always returns a single value The MegBox() and the InputBox() Are intrinsic functions supplied by VB Their function is to display messages and accept user input. For example, if you need to compute the square root of a user's entered value, you could write the assignments and expressions to compute the square root. The square root, however, is such a common routine that Microsoft wrote the code once and stored the square root routine in an intrinsic function. Now, if you want the square root of a value, you'll pass the value as a single argument to the square root function, and after performing the necessary math, the square root function will return the root. 12/30/2018 Prepared By: Deborah Becker
4
Prepared By: Deborah Becker
A MsgBox() We use a MsgBox() when we need a dialog box to give the user additional information Such as Error Messages Warnings You use input boxes and message boxes when you need to ask the user questions or display error messages and advice to the user. As stated earlier, the form's controls don't often work well for such user dialogs. For example, suppose the user is to enter a sales code of A, B, or C to indicate a discount to be used in a total calculation. Users don't always know what's expected of them, so a message box can pop up when the user enters a bad value and the message box can explain that the user needs to enter only A, B, or C. If the user enters an invalid code, your program could display an error message. 12/30/2018 Prepared By: Deborah Becker
5
Prepared By: Deborah Becker
An InputBox() An InputBox() is a dialog box you display to ask the user questions Visual Basic's controls just aren't enough to handle all the input that your program will need. Input boxes are great to use when the user must respond to certain kinds of questions. Text boxes and other controls are fine for getting fixed input from the user, such as data values with which the program will compute. Input boxes are great for asking the user questions that arise only under certain conditions. Input boxes always give the user a place to respond with an answer. Because the user can respond in several different ways, the program must be able to read the user response as well as responding to the cancel button. Note that there are two ways for the user to respond to the above input box. They can type in the needed information and click OK, or click the Cancel option. 12/30/2018 Prepared By: Deborah Becker
6
intVariable = MsgBox(strMsg[,[intType] [,strTitle]])
Examining MsgBox() Always assign the return value of a MsgBox() function to an integer variable This variable will hold the return value, and that value will indicate the button the user clicked *Note Message boxes can display multiple buttons such as NOTE: The MsgBox() function's format shown here accepts one required (strMsg) and two optional (intType and strTitle) arguments. MsgBox() can accept more arguments, but these three are the only ones needed in most applications. OK Cancel MsgBox()Function format intVariable = MsgBox(strMsg[,[intType] [,strTitle]]) 12/30/2018 Prepared By: Deborah Becker
7
Parts of MsgBox(), strMsg & intType
strMsg is a string that is—either A variable or (a string constant enclosed in quotation marks) And forms the text of the message displayed in the message box intType is optional It is a numeric value or expression That describes the message in the MsgBox() Visual Basic will not displays icons if you don't specify an intType value. If you want to use a value from two or more of the tables, you'll add the values together. Although you can use the integer value, if you use the built-in Visual Basic named literal, you'll more easily understand the message box's style if you ever have to change the message box in the future. strTitle is an optional string that represents the text in the message box's title bar. If you omit strTitle, Visual Basic uses the project's name for the message box's title bar text. The options that you select, using the intType value in the MsgBox() function, determine whether the message box displays an icon and controls the modality of the message box. 12/30/2018 Prepared By: Deborah Becker
8
Msgbox “Prompt”, type, title
vbOkOnly vbOkCancel vbRetryCancel vbYesNo vbYesNoCancel vbAbortRetryIgnore 12/30/2018 Prepared By: Deborah Becker
9
Prepared By: Deborah Becker
Example Dim iResponse As Integer iResponse% = MsgBox("Are you over, _ 18?", vbQuestion + vbYesNo, "Verify, _ Age") 12/30/2018 Prepared By: Deborah Becker
10
Prepared By: Deborah Becker
Modality New Term: Modality determines how the system handles a dialog box. It can either be: Application Specific Must respond to to continue application System Specific Must respond to use system at all The modality often causes confusion. If you don't specify a system-modal intType value of 4096 (or if you don't use the named literal vbSystemModal to specify the system's modal mode), the user's application will not continue until the user closes the message box, but the user can switch to another Windows program by pressing Alt+Tab or by switching to another program using the application's control menu. If, however, you do specify that the message box is system modal, the user will not be able to switch to another Windows program until the user responds to the message box because the message box will have full control of the system. Reserve the system-modal message boxes for serious error messages that you want the user to read and respond to before continuing the program. NOTE: If you don't specify an icon, Visual Basic doesn't display an icon. If you don't specify the system modality, Visual Basic assumes that you want an application-modal message box. 12/30/2018 Prepared By: Deborah Becker
11
Example The following MsgBox() function Produces
intPress = MsgBox("Are you ready for the report?", vbQuestion + _ vbYesNoCancel, "Report Request") Remember that the MsgBox() values such as vbQuestion and vbYesNoCancel are not variables but are named literals that Visual Basic has defined to correspond with matching integer values. The named literals vbQuestion and vbYesNoCancel produced both a question mark icon and the three buttons. The title of the message box is the third value inside the MsgBox() function. The reason that you assign MsgBox() functions to variables is so you can tell which button the user presses. Suppose that the user pressed the Yes button in Figure 6.4. The program could then print the report. If, however, the user pressed the No button, the program could describe what the user needed to do to get ready for the report (load paper, turn on the printer, and so on). If the user pressed the Cancel button, the program would know that the user didn't want the report at all. 12/30/2018 Prepared By: Deborah Becker
12
Examing InputBox() This function acts a lot like the MsgBox() function but: The InputBox() receives answers that are more complete The InputBox() function returns a string or Numeric data value that holds the answer typed by the user strPrompt works a lot like the strMsg value in a MsgBox() function. The user sees strPrompt inside the input box displayed on the screen. strTitle is the title inside the input box's title bar. strDefault is a default string value that Visual Basic displays for a default answer, and the user can accept the default answer or change the default answer. The intXpos and intYpos positions indicate the exact location where you want the input box to appear on the form. The intXpos value holds the number of twips from the left edge of the Form window to the left edge of the input box. The intYpos value holds the number of twips from the top edge of the Form window to the top edge of the input box. If you omit the intXpos and intYpos values, Visual Basic centers the message box on the form. InputBox() Format strVariable = InputBox(strPrompt[,[strTitle] [strDefault] _ A[,intXpos, intYpos]]]) 12/30/2018 Prepared By: Deborah Becker
13
Prepared By: Deborah Becker
InputBox() NOTE: Input boxes always contain OK and Cancel command buttons. If the user clicks OK (or presses Enter, which selects OK by default), the answer in the input box is sent to the variable being assigned the returned value. If the user clicks Cancel, a null string ("") returns from the InputBox() function. 12/30/2018 Prepared By: Deborah Becker
14
Example of the InputBox()
strCompName = InputBox("What is the _ name of the company?", "Company _ Request", "XYZ, Inc.") TIP: You can offer a default answer that the user can accept or change in the strDefault argument. The input box function returns the answer to the string variable to which you assign the function. 12/30/2018 Prepared By: Deborah Becker
15
Prepared By: Deborah Becker
Summary Message boxes display output, and input boxes get input. Use message and input boxes to get data that regular controls can't handle. Use controls to display and get data values that are always needed. Use message and input boxes to display messages and get answers that the program needs in special cases, such as for error conditions and exception handling. 12/30/2018 Prepared By: Deborah Becker
16
Checking an InputBox() return value.
12/30/2018 Prepared By: Deborah Becker
17
Prepared By: Deborah Becker
Input Validation Checking the value of the text boxes IsNumeric() Checking for a range of values If Val(txtHours.Text) > 10 then MsgBox(“Too Many Hours Entered”, _ vbOKOnly, “Invalid Data”) End if You can also check for required fields When checking the value enter into a text box for numeric input you should use the IsNumeric() function. If the input is a numeric value the function returns true, if not it returns false—This is where you would place a MsgBox() function to display an error message or on InputBox() functions to give error checking instructions. 12/30/2018 Prepared By: Deborah Becker
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.