Anda di halaman 1dari 4

Chapter 7 Homework Solutions:

Chapter 7-Lists, Loops, and Printing ANSWERS TO REVIEW QUESTIONS 1. What is a list box? a combo box? List box controls and combo box controls display a list of items from which the user can make a selection. List boxes and combo boxes have most of the same properties and operate in a similar fashion. One exception is that a combo box control has a Style property, which determines whether or not the list box also has a text box for user entry and whether or not the list will drop down. Another exception is that combo boxes have a Text property that is available at design time: the Text property of list boxes is available only during program execution. Name and describe the three styles of combo boxes. The dropdown combo box (Style 0) has a dropdown list with a text box and the list collapses when the user makes a selection from the list. The simple combo box (Style 1) has a text box and the list displays below with scroll bars, if needed. The dropdown list (Style 2) allows the user to select from an existing list. The dropdown list does not have a text box. How can you make scroll bars appear on a list box or combo box? Both list boxes and combo boxes will add a scroll bar automatically if there are more items in the list than can be seen. The location of the scroll box in the scroll bar and all of the scrolling is handled automatically. Explain the purpose of the ListIndex property and the ListCount property. When a project is running and the user selects an item from the list, the index number (the items position in the list) of that item is stored in the ListIndex property of the list box. The ListIndex is used to select an item in the list or deselect all items in the list (ListIndex = -1). The ListCount property of a list box or combo box is used by the application to store the number of items in the list. The ListCount is always one more than the highest ListIndex, since ListIndex begins with zero. When and how is information placed inside a list box or a combo box? There are several ways to fill a list box or a combo box. If you know the list contents at design time, and the list never changes, you can define the list items in the Properties window. If you must add items to the list during program execution, you will use the AddItem method in an event procedure. In what situation would a loop be used in a procedure? A loop would be used when an instruction or a group of instructions needs to be repeated within a procedure. Explain the difference between a pretest and a posttest in a Do/Loop. A pretest tests for completion at the top of the loop. With this type of loop, the statements inside the loop may never be executed if the terminating condition is true the first time it is tested. intTotal = 0 Do Until intTotal = 0

2.

3.

4.

5.

6.

7.

'statements in loop Loop A posttest tests for completion at the bottom of the loop, which means that the statements in the loop will always be executed at least once. Do 'statements in loop Loop Until intTotal = 0 8. Explain the differences between a Do/Loop and a For/Next loop. For/Next loops are used when you know the number of iterations needed for the loop. The loop index determines the number of times the statements inside the loop will be executed. A Do/Loop terminates the loop only when a specified condition is satisfied. Do/Loops are used when the exact number of iterations is unknown. What are the steps in processing a For/Next loop? The general form for For/Next Statements is: For LoopIndex = InitialValue To TestValue [Step Increment] (Body of loop) Next [LoopIndex] When the For statement is reached during program execution several things occur. The loop index is established as the loop counter and is set to the initial value shown after the equal sign. Execution is now controlled by the For statement. After the value of the LoopIndex is set, it is tested to see if the LoopIndex is greater than TestValue. If not, the statements in the body of the loop are executed. The Next statement causes the LoopIndex to be incremented by 1, unless a Step Increment is specified. The control passes back to the For statement. When the test is made and the loop index is greater than the Test Value, control passes to the statement immediately following the Next. A counter-controlled loop generally has three elements 1. Initialize the counter. 2. Increment the counter. 3. Test the counterto determine when it is time to terminate the loop. Discuss how and when the values of the loop index change throughout the processing of the loop. The values of the loop index are controlled by a counter throughout the processing of the loop. First, the counter is initialized to the initial value specified in the For statement. When the Next statement is reached, the counter is incremented by 1 (or the value shown by the Step in the For statement). Back again at the For statement, the counter is tested against the TestValue in the For statement. When the counter is greater than the test value, the loop is terminated. What is the purpose of Printer.Print? The Printer.Print statement sends lines to the printer, rather than printing the graphical form. It is possible to send whatever output you have assembled and chosen to print. Using Printer.Print (Object.Method) Visual Basic sets up a Printer object in memory and sends your output there. Then, when your job terminates, or it receives an EndDoc or NewPage method, VB actually sends the contents of the Printer object to the printer

9.

10.

11.

12.

How do the Left, Right, and Len functions operate? The Left and Right functions return the specified section of a string, rather that the entire string. The format for the Left and Right functions are the same. Left(StringExpression, Number of Characters) Right(StringExpression, Number of Characters) StringExpression in each of these statement may be a string variable, string literal, or text property. NumberOfCharacters is numeric and may be variables, literals, or numeric expressions. The Len function is used to determine the length of a string expression. The value returned by the Len function is an integer count of the number of characters in the string. Len(StringExpression) How do you control the horizontal spacing of printer output? The Print method uses commas, semicolons, and the Spc function to control spacing. The Tab function can be used to align columns of information. Commas advance printing to the next print zone. There are 5 print zones, or columns on a page. Semicolons allow items to be separated without advancing to the next print zone. The Tab function controls the exact column position where the output will begin. The Spc function differs from the Tab function in that you specify the number of spaces on the line that you want to advance from the last item printed.

13.

Anda mungkin juga menyukai