0% found this document useful (0 votes)
55 views

Lists, Loops, and Printing: Filling The List

This document discusses various topics related to lists, loops, and printing in Visual Basic, including: 1) How to fill lists using the list property or addItem method, clear lists, and remove items. It also covers list-related properties like listIndex and listCount. 2) Details about do/loops and for/next loops, including using Boolean variables and exiting loops. 3) How to use the MsgBox function and common message box constants. 4) String functions like Left, Right, Mid, and Len to manipulate strings. 5) How to print to a printer using the Printer object's Print method and format print lines.

Uploaded by

vineevinay
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Lists, Loops, and Printing: Filling The List

This document discusses various topics related to lists, loops, and printing in Visual Basic, including: 1) How to fill lists using the list property or addItem method, clear lists, and remove items. It also covers list-related properties like listIndex and listCount. 2) Details about do/loops and for/next loops, including using Boolean variables and exiting loops. 3) How to use the MsgBox function and common message box constants. 4) String functions like Left, Right, Mid, and Len to manipulate strings. 5) How to print to a printer using the Printer object's Print method and format print lines.

Uploaded by

vineevinay
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

Chapter 7 Lists, Loops, and Printing

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.

Filling the list


Using properties window

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.

Clearing the list


During run time you can also clear the list Object.clear

The listindex property


When the project is running the user selects an item from the list, the index number of that item is stored in the listIndex property of the list box. If no item is selected this property will be set to -1. you can use this property to select and deselect items in code. Object.listIndex =3 Object.listIndex = -1

The listcount property


This property keeps the count of the items in the list. Itotalitems = lstitem.ListCount
Removing an item from the list

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.

Using the Boolean data type in loops


You will find Boolean variables very useful when testing conditions for a loop. An example of using a Boolean varialbel is whne you want to search through a list for a specific value. The item may be found or not found, and you want to quit looking when a match is found. Using a Boolean variable is a three step process. First you must dimension a variable and set its initial value( the default VB setting is false)

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

For loopIndex = InitialValue to TestValue (Step Increment) body of the loop

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

Constant vbOk vbCancel vbAbort vbRetry vbIgnore vbyes vbNo

value 1 2 3 4 5 6 7

Button Pressed OK Cancel Abort Retry Ignore Yes No

Specifying the button and/or Icons to display

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

Constant vbOkonly vbOkCancel vbAbortRetryIgnore vbYesNoCancel vbYesNo

Retry and Cancel Critical message Warning Query Warning message Information message

5 16 32 48 64

vbRetryCancel vbCritical vbQuestion vbExclamation vbInformation

Using String Function


When you need to look at part of a string, rather than the entire string, vb provides the Left, Right, Mid functions that return the specified section of a string. Left(StringExpression, NoOfCharacters) Right(StringExpression, NoOfCharacters) Mid(StringExpression, StartPosition [, NoOfCharacters]) StringExpression may be a string variable, string literal or text property. StartPosition and NoOfCharacters are both numeric and may be variables, literals, or numeric expressions. In the mid function, if you omit the number of characters argument, the function returns all characters starting with startposition. Left(txtName.text, 5) returns first 5 characters Right(strLongString, 1) returns last one characters Mid(Mad Matter,5,3) returns 3 characters begging with character 5 Mid(strProductID, 4) returns all characters beginning with character 4

The len function


Len function can be used to determine the length of a string expression. Len(stringExpression) The value returned by the len function is an integer count of the number of characters in the string.

Sending information to printer


The print form method is used for printing the form, but the output is graphic and does not produce attractive text. In addition to printing forms you have to produce reports or print small bits of information on the printer. You can use VBs Print method to print text on a form, on the printer object, or in the debug window. Visual basic was designed to run under windows, which is a highly interactive environment. It is extremely easy to create for interactive programs using visual basic, but not easy at all to print on the printer. Most professionals use a separate utility program to format printer reports. The vb professional addition and enterprise edition include a data report designer for creating reports from database files.

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

The tab function

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.

You might also like