Lists, Loops, and Printing: Filling The List
Lists, Loops, and Printing: Filling The List
Both list box controls and combo box controls allow you to have a list of items from which the user can make a selection. Most of the properties for both of them are the same. Combo boxes got a style property which determines whether or not the list box has a text box for user entry and whether or not the list will drop down. Combo boxes also got a text property that is available at design time. The text property of list boxes is available only during program execution. If the list box or combo box is too small to hold all items, scroll bars will appear automatically and the scrolling is handled automatically.
The list property holds the list of items for a list box or combo box. Go to the list property and enter the list move to the next item by pressing ctrl + enter.
Using the additem method
To add item from code use additem method. Object.additem value [,index] Value is the value to be added to the list. If the value is string enclose it in quotation mark. The optional index identifies the position within the list to place the new item. The first element in the list is 0. if you omit the index the new item will be added at the end of the list. But you can change the placement by setting the sorted property to true.
Object.removeitem index
Events
The change event: as the user types text into the text box portion of a combo box the change event occurs. Each keystroke generates another change event. This event will be used later to match the characters typed to the elements in a list. A list box does not have the change event, because list boxes do not have associated text boxes. The gotFocus event: when the user tabs to the control a gotfocus event fires. You can use to make any existing text appear selected when the user tabs to a text portion of a combo box.
The lost focus event: this event fires as the control loses the focus. It is used often to validate the data.
Do/Loops
The process of repeating a series of instructions is called looping. The group of repeated instructions is called a loop. Iteration is a single execution of the statements in the loop. A do/loop terminates based on a condition that you specify. Execution of a do/loop continues while the condition is true or until a condition is true. You can place the condition at the top or the bottom of the loop. Use a do/loop when the exact number of iterations is unknown. Align the do and loop statements with each other indent the lines of code to be repeated in between. Do (While| Until) Condtion statements in the loop loop or Do statements in the loop Loop (While | Until) Condition Ex. intTotal =0 do Until intTotal = 0 statements loop Because intTotal is 0 the first time the condition is tested, the condition is true and the statements inside the loop will not execute. Control will pass to the statements after the loop statement. intTotal =0 do statements loop Until intTotal = 0 in this case the statements inside the do loop will be executed at least once. Assuming the value of intTotal does not change, the condition will be true the first time it is tested and control will pass to the next statement following the loop statement.
Then a particular situation occurs, set the variable to true. A loop condition can then check for true. Dim blnItemFound as Boolean blnItemFound = false Do until blnItemFound checks for true
look for a match between the text box and list elements
Dim blnItemFound as Boolean Dim intItemIndex as Integer Do until blnItemFound or IntItemIndex = lstItems.ListCount If txtNewItem.Text = lstItems.list(IntIntemIndex) then blnItemFound = True end if intItemIndex = intItemIndex +1 loop if blnIntemFound then msgbox item is in the list else msgbox item is not in the list end if
For/Next loops
When you want to repeat the statements in a loop a specific number of times, the For/Next loop is ideal. The for/Nest loop use the For and Next Statements and a counter variable, called the loop index the loop index determines the number of times the statements inside the loop will be executed. Dim IntLoopIndex as Integer Dim intMaximum as Integer intMaximum = lstSchools.ListCount -1 for intloopIndex = 0 to intMaximum statements next intLoopIndex The loop index, intLoopIndex, is established as the loop counter and initialized to 0. The final value for the loop index is set to the value of intMaximum, which was assigned the value of lstSchools.ListCount -1 in the previous statement. Execution is now controlled by the for statement. After the value of intLoopIndex is set, it is tested to see whether intLoopIndex is greater than intMaximum. If not, the statement of loop body is executed. The Next statement causes the intLoopIndex to be incremented by 1. The control passes back to the For statement. When the test is made the loop index is greater than the maximum, control passes to the statement after the next statement.
General form
Next [LoopIndex] You can use a negative increment for the step increment to decrease the loop index rather than increase it. When the step is negative, VB tests for less than the test value instead of greater than. For intCount = 10 to 1 step -1 statements next if you change the loop variables inside the for loop, it got no effect on the loop. VB just ignores it. But if you change the index it takes effect, is poor programming. If you initialize the index again within the loop, it will result in endless loop To exit for loop you can use Exit For With in a if clause
Msgbox function
Msgbox (prompt, [, Buttons] [,Title] Ex. Dim intresponse as integer Intresponse = msgbox (do you wish to continue? , vbYesNo + vbQuestion, Title) If intresponse = vbyes then statements end if
Message box function return values
value 1 2 3 4 5 6 7
You can specify which buttons to display by using numbers or visual basic intrinsic constants. If you want to choose the buttons and icons, use a plus sign to add the two values together.
Buttons and icon values
Buttons to display OK OK and Cancel Abort, Retry,Ignore Yes, No and Cancel Yes and No
value 0 1 2 3 4
Retry and Cancel Critical message Warning Query Warning message Information message
5 16 32 48 64
Printing to printer
You can set up output for the printer using printer.print . VB establishes a printer object in memeory for your output. Each time you issue a printer.print method, your output is added to the printer object. When your job terminates or it receives an enddoc or newpage method, vb sends the contends fo the printer object to the printer.
Formatting lines
The format a print line uses punctuation, the coma and semicolon, as well as a tab function and a spc function. When an output contains multiple items to be printed the items will be separated by coma or semicolons. The list of items may contain literals, variables, or the contents of objects. When you print on the printer, it is important to consider the font being used. Normally, printing is done with proportional fonts, which means that the amount of space for one character varies with the character. For example a W takes more space than i. You can use fixed pitch font, such as Courier, if you want every character to take the same amount of space.
Commas
The output page has preset tab settings with five columns per line, each colum is referred to as a print zone. Use a comma to advance to next print zone. Printer.print, R n R Prints the string int second print zone. You can use two commas to advance two print zones. Printer. Print Name, ,Phone The width of the print zone is 14 characters, based on the average size of character for the font being used. If you use very narrow characters, like I and t, more than 14 characters will fit. If characters are wide then fewer than 14 characters will fit in a print zone. Print zone represents the tab character in a word processor. If some text exeeds a print zone, a comma will jump to the next print zone.
Semicolons
If you need to separate items without advancing to the next print zone, use a semicolon between the two items. If you leave spaces between items, vb adds a semicolon in the code for you. Printer.print Name : ; txtname.text Will output as Name : Mary Note that the number of spaces left inside the string literal will print out exactly as indicated. If you do not put spaces inside the quotes, you may well end up with one item printing right next to the previous item. The semicolon does not provide spaces in the output, only a means of listing items to print.
Trailing commas and semicolons
It the last character in a line is a comma or semicolon, next print statement will print on the same line. Printer.print First this, Printer.print Then this The output will be First this Then this The print zone rule is still in effect because the first Print method ended with a trailing comma.
Blank lines
When you want a blank line, use the printer method without any item to print Printer.print
You can control the placement of variables and constants on a printed line using tab function. Printer.print tab(20); this text This will give twenty average character width before the text.
Spc function
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. Printer.print tab(20); Name; spc(5);phone
Aligning string and numeric data
Numeric data prints with additional spacing, VB follows one space before each number for a sign. For negative values, a minus sign appears in the position; for positive values, the space remains blank. VB also adds a space followed by each numeric value. Printer.print 1; 2; -1; -2 Output is 1 2 -1 -2 printer.print item, quantity printer.print scissors,10 printer.print Rocks, -2 output is Item quantity Scissors 10 Rocks -2
Selecting the font
The printer object got a font property, which refers to the font object. You must change this properties in code. Printer.Font.name = Times New Roman Printer.Font.size =12 You change the properties only once before the first item of print.
Terminating a print job
The new page mehthod sends the current page to the printer and clears the printer object in memory so you can begin a new page. The enddoc method sends the current page to the printer and terminates the printer job. When your program terminates VB automatically sends an endDoc Printer.newpage Printer.enddoc
Right aligning
Printer.print tab(50- len(strformattedNumber)); strformattedNumber Ex. Create a project for R n R for reading refreshment that contains dropdown box of the coffee flavors and a list box of syrup flavors. Adjust the size of the boxes as needed when you test the project. The user should be able to add more coffee flavors to the list. Include a menu item to print all the flavors on the printer and another to print only a selected item from each list. Add this print commands in File menu along with exit command.
In edit menu add items, add coffee flavor remove coffee flavor, clear coffee list, display coffee count. Add a help menu with about. Modify the add coffee flavor so that duplicates are not added.