Chapter 7 Lists Loops and Printing Programming In

  • Slides: 40
Download presentation
Chapter 7 Lists, Loops, and Printing Programming In Visual Basic. NET © 2004 by

Chapter 7 Lists, Loops, and Printing Programming In Visual Basic. NET © 2004 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

List Boxes and Combo Boxes • Provide the user with a list of items

List Boxes and Combo Boxes • Provide the user with a list of items to select from • Various styles, choose based on – Space available – Need to select from an existing list – Need to add to a list 2 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

List Boxes and Combo Boxes (continued) • List. Box control – Simple List Box

List Boxes and Combo Boxes (continued) • List. Box control – Simple List Box with/without scroll bars • Combo. Box control – List may allow for user to add new items – List may "drop down" to display items in list 3 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Various Styles of List Boxes and Combo Boxes Dropdown Combo Box List Boxes Simple

Various Styles of List Boxes and Combo Boxes Dropdown Combo Box List Boxes Simple Combo Box Dropdown List Box 4 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

The Items Collection • List of items in a List. Box or Combo. Box

The Items Collection • List of items in a List. Box or Combo. Box is a collection • Collections are objects that have properties and methods that allow you to – Add items – Remove items – Refer to individual elements – Count items – Clear the collection 5 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Filling a List • Design time in Properties window – Items property – Click

Filling a List • Design time in Properties window – Items property – Click on ellipses to open String Collection Editor – Type list items, end each line with ENTER key • Run time methods – Items. Add OR – Items. Insert 6 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Filling a List - Design Time Click ellipses button to open 7 © 2005

Filling a List - Design Time Click ellipses button to open 7 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Items. Add Method • Use to add new items to the list at run

Items. Add Method • Use to add new items to the list at run time • General Form Object. Items. Add(Item. Value) • Examples schools. List. Box. Items. Add("Harvard") schools. List. Box. Items. Add("Stanford") schools. List. Box. Items. Add(schools. Text. Box. Text) majors. Combo. Box. Items. Add(majors. Combo. Box. Text) majors. Combo. Box. Items. Add(major. String) 8 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Items. Insert Method • Use to add new items to the list at run

Items. Insert Method • Use to add new items to the list at run time in a specific location (index) in the collection • General Form Object. Items. Insert(Index. Position, Item. Value) • Examples schools. List. Box. Items. Insert(0, "Harvard") majors. Combo. Box. Items. Insert(1, majors. Combo. Box. Text) 9 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

The Selected. Index Property • Index number of currently selected item is stored in

The Selected. Index Property • Index number of currently selected item is stored in the Selected. Index property • If no list item is selected, Selected. Index property is negative 1 (-1) • Use to select an item in list or deselect all items in code 10 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

The Items. Count Property • Use to determine number of items in the list

The Items. Count Property • Use to determine number of items in the list Remember: Items. Count is always one more than the highest possible Selected Index because indexes begin with 0 For example, if there are five items in a list: Items. Count = 5 AND Highest Index = 4 11 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Referencing the Items Collection • Use the index of the item to reference a

Referencing the Items Collection • Use the index of the item to reference a specific item in the collection • Remember that the index is zero based so the first item in the list is index position zero schools. List. Box. Items(5) = "University of California" ' Next line references the currently selected item. selected. Flavor. String = flavor. List. Box. Items(flavor. List. Box. Selected Index). To. String( ) 12 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

The Items. Remove. At Method • Use the Items. Remove. At method to remove

The Items. Remove. At Method • Use the Items. Remove. At method to remove an item by index from the list • General Form Object. Items. Remove. At(Index. Position) • Examples names. List. Box. Items. Remove. At(0) schools. Combo. Box. Items. Remove. At(index. Integer) ' Next line removes the currently selected item. coffee. Combo. Box. Items. Remove. At(coffee. Combo. Box. Selected. Index) 13 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

The Items. Remove Method • Use the Items. Remove method to remove an item

The Items. Remove Method • Use the Items. Remove method to remove an item by specifying the text • General Form Object. Items. Remove(Text. String) • Examples names. List. Box. Items. Remove("My School") schools. Combo. Box. Items. Remove(school. Text. Box. Text) ' Next line removes the currently selected item. coffee. Combo. Box. Items. Remove(coffee. Combo. Box. Text) 14 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Clearing a List • Use the Items. Clear method to clear all items and

Clearing a List • Use the Items. Clear method to clear all items and empty a combo box or list box • General Form Object. Items. Clear( ) • Examples schools. List. Box. Items. Clear( ) majors. Combo. Box. Items. Clear( ) 15 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

List Box and Combo Box Events • Text. Changed Event – Occurs when user

List Box and Combo Box Events • Text. Changed Event – Occurs when user types text into combo box – List box does not have Text. Changed Event • Selected. Index. Changed Event • Enter Event (receive focus) • Leave Event (lose focus) 16 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Do/Loops • Repeating a series of instructions • An iteration is a single execution

Do/Loops • Repeating a series of instructions • An iteration is a single execution of the statement(s) in the loop • Used when the exact number of iterations is unknown 17 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Do/Loops (continued) • Terminates based on a specified condition – Loop While a condition

Do/Loops (continued) • Terminates based on a specified condition – Loop While a condition is True – Loop Until a condition becomes True • Condition can be placed at – Top of loop - Pretest – Bottom of loop - Posttest 18 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

The Do and Loop Statements General Form Do {While |Until} condition ' Statements in

The Do and Loop Statements General Form Do {While |Until} condition ' Statements in loop. Loop Top of Loop Condition, Pretest OR Do ' Statements in loop. Loop {While | Until} condition 19 Bottom of Loop Condition, Posttest © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Pretest vs. Posttest • Pretest, loop may never be executed since tested BEFORE running

Pretest vs. Posttest • Pretest, loop may never be executed since tested BEFORE running Do While … Loop Do Until … Loop • Posttest, loop will always be executed at least once Do … Loop While Do … Loop Until 20 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

The Boolean Data Type Revisted • Can help when searching a list for a

The Boolean Data Type Revisted • Can help when searching a list for a specific value – Dimension a variable and set initial value – When a particular situation occurs, set variable to True – Use a loop to check for True 21 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

For/Next Loops • Use when you know the number of iterations • Uses a

For/Next Loops • Use when you know the number of iterations • Uses a numeric counter variable, called Loop Index, to control number of iterations • Loop Index is incremented at the bottom of the loop on each iteration • Step value can be included to specify the incrementing amount to increment Loop Index, step can be a negative number 22 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

The For and Next Statements General Form For Loop. Index = Initial. Value To

The For and Next Statements General Form For Loop. Index = Initial. Value To Test. Value [Step Increment] ' Statements in loop. Next [Loop. Index] 23 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Exiting For/Next Loops • In some situations you may need to exit the loop

Exiting For/Next Loops • In some situations you may need to exit the loop prematurely • Use the Exit For statement inside the loop structure • Generally the Exit For statement is part of an If statement 24 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Making Entries Appear Selected • When a user tabs into a text box that

Making Entries Appear Selected • When a user tabs into a text box that already has an entry, the user-friendly approach is to select the text • If a text box fails validation, select the text 25 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Making Entries Appear Selected (continued) • Selecting the entry in a Text Box –

Making Entries Appear Selected (continued) • Selecting the entry in a Text Box – Use the Select. All method – Good location is in the text box’s Enter event • Selecting an entry in a List Box – Set the Selected. Index property – See the code example on page 296 which selects matching entries from the list as the user types 26 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Sending Information to the Printer • Most programmers use a separate utility program to

Sending Information to the Printer • Most programmers use a separate utility program to format printer reports • Crystal Reports, packaged with the VB Professional and Enterprise Editions, creates reports from database files 27 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

The Print. Document Component • Add a Print. Document component to form – Appears

The Print. Document Component • Add a Print. Document component to form – Appears in the Component Tray • Execute the Print method to start printing • Logic for actual printing belongs in the Print. Document's Print. Page event procedure 28 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Setting Up the Print Output • Print. Page event is fired once for each

Setting Up the Print Output • Print. Page event is fired once for each page to be printed, referred to as callback • Begin. Print and End. Print are also fired at the beginning and end of the printing • Print. Page event includes the argument e as System. Drawing. Print. Page. Event. Args • Properties of the Print. Page. Event. Args are useful for handling page margins and sending strings of text to the page 29 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

The Graphics Page • Set up graphics page in memory and then page is

The Graphics Page • Set up graphics page in memory and then page is sent to the printer • Can contain strings of text and graphic elements • Specify the exact X and Y coordinates of each element to be printed on the page 30 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Using the Draw. String Method • Used to send a line of text to

Using the Draw. String Method • Used to send a line of text to the graphics page • Belongs to the Graphics object of the Print. Page. Event. Args argument • Is an overloaded method so there are several forms for calling the method • Set up the Font to be used before executing the Draw. String method 31 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

The Draw. String Method (cont) General Form Draw. String(String. To. Print, Font, Brush, Xcoordinate,

The Draw. String Method (cont) General Form Draw. String(String. To. Print, Font, Brush, Xcoordinate, Ycoordinate) Examples e. Graphics. Draw. String(print. Line. String, print. Font, Brushes. Black, _ horizontal. Print. Location. Single, vertical. Print. Location. Single) e. Graphics. Draw. String("My text string", my. Font, Brushes. Black, _ 100. 0, 100. 0) e. Graphics. Draw. String(name. Text. Box. Text, New Font("Arial", 10), _ Brushes. Red, left. Margin. Single, current. Line. Single) 32 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Setting the X and Y Coordinates • For each print line, specify X and

Setting the X and Y Coordinates • For each print line, specify X and Y coordinates • Create variables declared as Single to set the X and Y values 33 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Print. Page. Event. Args • Print. Page. Event. Args argument has several useful properties

Print. Page. Event. Args • Print. Page. Event. Args argument has several useful properties • Margin. Bounds – Code as • e. Margin. Bounds. Left • e. Margin. Bounds. Right • e. Margin. Bounds. Top • e. Margin. Bounds. Bottom • Page. Bounds • Page. Settings 34 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Aligning Decimal Columns • It is important to align the decimal points of numeric

Aligning Decimal Columns • It is important to align the decimal points of numeric data • Proportional fonts make aligning decimal points difficult • Declare an object as a Size. F Structure • Use Measure. String method of the Graphics class to determine the width of a formatted string in pixels 35 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Aligning Decimal Columns Code Example ' Size. F structure for font size info. Dim

Aligning Decimal Columns Code Example ' Size. F structure for font size info. Dim font. Size. F As New Size. F( ) ' Set X for left-aligned column. horizontal. Print. Location. Single = 200 ' Set ending position for right-aligned column. End. Single = 500 ' Format the number. formatted. Output. String= amount. Decimal. To. String("C") ' Calculate the X position of the amount. ' Measure string in this font. Size. F= e. Graphics. Measure. String(formatted. Output. String, _ print. Font) 36 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Aligning Decimal Columns Code Example (cont) ' Size. F structure for font size info

Aligning Decimal Columns Code Example (cont) ' Size. F structure for font size info (cont). ' Subtract width of string from the column position. column. XSingle = column. End. Single - font. Size. F. Width ' Set up the line--each element separately. e. Graphics. Draw. String("The Amount = ", print. Font, _ Brushes. Black, horizontal. Print. Location. Single, _ vertical. Print. Location. Single) e. Graphics. Draw. String(formatted. Output. String, print. Font, _ Brushes. Black, column. XSingle, vertical. Print. Location. Single) ' Increment line for next line. vertical. Print. Location. Single += line. Height. Single 37 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Displaying a Print Preview • Add Print. Preview. Dialog component to form – Appears

Displaying a Print Preview • Add Print. Preview. Dialog component to form – Appears in the Component Tray – Default name is fine • Assign in code the same Print. Document object you are using for printing • Execute the Show. Dialog method of the Print. Preview. Dialog component 38 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Using Static Variables • Static local variables retain their value for the life of

Using Static Variables • Static local variables retain their value for the life of the project • Can be useful for – Running totals – Running counts – Boolean switches – Storing current page number/count when printing multiple pages 39 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Printing Multiple Pages • Recall that the Print. Document's Print. Page event fires once

Printing Multiple Pages • Recall that the Print. Document's Print. Page event fires once for each page • Indicate that there are more pages to print by setting the Has. More. Pages property of the Print. Page. Event. Args to True 40 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.