Input Dialog Box An input dialog box can be used to obtain a single item of input from the user Presents a window (dialog box) requesting input Syntax: stringVar = InputBox(prompt, title)
Example of an Input Dialog Box Private Sub cmdDisplay_Click() Dim fileName As String, prompt As String, title As String Dim houseNumber As Single, street As String prompt = "Enter the name of the file containing the information." title = "Name of File" fileName = InputBox(prompt, title) Open fileName For Input As #1 Input #1, houseNumber Input #1, street picAddress.Print "The White House is at"; houseNumber; street Close #1 End Sub After executing, an input dialog box will pop up
Using Message Dialog Box for Output The message dialog box is used to present a pop-up window containing information for the user Syntax: MsgBox prompt,, title
Example of a Message Dialog Box MsgBox “Nice try, but no cigar”,, “Consolation” Stays on the screen until the user presses OK
Formatting the Output: Create easily readable output In the Print method, the spacing of the output is controlled by the following devices: semicolon comma Tab function
Semicolons The next value output is placed in the next column position. Example: picOutput.Print “Patrick”; ”Jon” Output: PatrickJon
Example of Semicolon picOutput.Print “Patrick”; “ Jon” Output Screen: Patrick Jon Space here
Example of Semicolon picOutput.Print 100; -200; 300 Output Screen: One space Two spaces
Commas A comma in a Print method causes the next value output to be placed in the next available print zone. Each print zone is 14 positions wide.
Using Commas Example: picOutput.Print “SEE”, ”YOU”, ”SOON” Output Screen: SEE YOU SOON Column 1 Column 15 Column 29
Using Commas A print zone can be skipped by typing consecutive commas Example: picOutput.Print “HOURLY”,, “PAY” Output Screen: HOURLY PAY Column 29
Tab Function Specifies the column where output will start Use only semicolons with the Tab function Can only be used to advance the print position (cannot move backwards)
Example of Tab Function Example: picOutput.Print Tab(3); “Hi there!” ; Tab(25) ;“Bye!” Output Screen: Hi there! Bye! Column 3 Column 25
Built-In Functions Take one or more input values and return an output value A means provided by Visual Basic for carrying out small, common tasks Types of Built-In functions Numeric functions (manipulate numbers) String functions (manipulate strings)
Numeric Functions
Example of Numeric Functions Private Sub cmdEvaluate_Click() Dim n As Single, root As Single n = 6.76 root = Sqr(n) picResults.Print root; Int(n); Round(n,1) End Sub Output:
Commonly-Used String Functions Function: Left(“Penguin”, 4) Purpose: Returns the number of characters specified, starting at the beginning of the string
Commonly-Used String Functions Function: Right(“Cork City”, 4) Purpose: Returns the number of characters specified from the end of the string
Commonly-Used String Functions Function: Mid(“Commissioner”, 4, 3) Purpose: Returns the substring starting at the position indicated by the first number and continuing for the length specified by the second number
Commonly-Used String Functions Function: UCase(“Yes”) Purpose: Converts any lowercase letters in a string to uppercase
String-Related Numeric Functions Function: InStr(“John Smith”, “m”) Purpose: Searches for the first occurrence of one string in another and gives the position at which the string is found
String-Related Numeric Function Function: Len(“John Smith”) Purpose: Returns the number of characters in the string.
Format Functions The format functions provide detailed control of how numbers, dates, and strings are displayed. Examples FormatNumber ( , 1) 12,345.6 FormatCurrency ( , 2) $12, FormatPercent (.185, 2) 18.50% FormatNumber (1 + Sqr(2), 3) 2.414
Format Function Format (expr, Purpose: The value of this function is the value of expr right justified in a field of n spaces, where n is the number symbols.
Format Examples Format(12345, Format(123, 123 Format(“123.4”, 123.4
FormatDateTime Example FormatDateTime (“ ”, vbLongDate) Output: Monday, September 15, 2004
Rnd Function Returns a random number from 0 to 1. (excluding 1). Example: picBox.Print Int(6 * Rnd) + 1 Output: Displays a random integer from 1 through 6.