Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7 Lists, Loops, and Printing

Similar presentations


Presentation on theme: "Chapter 7 Lists, Loops, and Printing"— Presentation transcript:

1 Chapter 7 Lists, Loops, and Printing
Programming In Visual Basic .NET

2 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

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

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

5 The Items Collection List of items in a ListBox or ComboBox 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

6 Filling a List Design time in Properties window Run time methods
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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

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

8 Items.Add Method Use to add new items to the list at run time
General Form Examples Object.Items.Add(ItemValue) schoolsListBox.Items.Add("Harvard") schoolsListBox.Items.Add("Stanford") schoolsListBox.Items.Add(schoolsTextBox.Text) majorsComboBox.Items.Add(majorsComboBox.Text) majorsComboBox.Items.Add(majorString) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

9 Items.Insert Method Use to add new items to the list at run time in a specific location (index) in the collection General Form Examples Object.Items.Insert(IndexPosition, ItemValue) schoolsListBox.Items.Insert(0, "Harvard") majorsComboBox.Items.Insert(1, majorsComboBox.Text) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

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

11 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

12 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 schoolsListBox.Items(5) = "University of California" ' Next line references the currently selected item. selectedFlavorString = flavorListBox.Items(flavorListBox.Selected Index).ToString( ) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

13 The Items.RemoveAt Method
Use the Items.RemoveAt method to remove an item by index from the list General Form Examples Object.Items.RemoveAt(IndexPosition) namesListBox.Items.RemoveAt(0) schoolsComboBox.Items.RemoveAt(indexInteger) ' Next line removes the currently selected item. coffeeComboBox.Items.RemoveAt(coffeeComboBox.SelectedIndex) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

14 The Items.Remove Method
Use the Items.Remove method to remove an item by specifying the text General Form Examples Object.Items.Remove(TextString) namesListBox.Items.Remove("My School") schoolsComboBox.Items.Remove(schoolTextBox.Text) ' Next line removes the currently selected item. coffeeComboBox.Items.Remove(coffeeComboBox.Text) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

15 Clearing a List Use the Items.Clear method to clear all items and empty a combo box or list box General Form Examples Object.Items.Clear( ) schoolsListBox.Items.Clear( ) majorsComboBox.Items.Clear( ) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

16 List Box and Combo Box Events
TextChanged Event Occurs when user types text into combo box List box does not have TextChanged Event SelectedIndexChanged Event Enter Event (receive focus) Leave Event (lose focus) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

17 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

18 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

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

20 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

21 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

22 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

23 The For and Next Statements - General Form
For LoopIndex = InitialValue To TestValue [Step Increment] ' Statements in loop. Next [LoopIndex] © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

24 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

25 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

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

27 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

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

29 Setting Up the Print Output
PrintPage event is fired once for each page to be printed, referred to as callback BeginPrint and EndPrint are also fired at the beginning and end of the printing PrintPage event includes the argument e as System.Drawing.Printing.PrintPageEventArgs Properties of the PrintPageEventArgs are useful for handling page margins and sending strings of text to the page © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

30 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

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

32 The DrawString Method (cont)
General Form DrawString(StringToPrint, Font, Brush, Xcoordinate, Ycoordinate) Examples e.Graphics.DrawString(printLineString, printFont, Brushes.Black, _ horizontalPrintLocationSingle, verticalPrintLocationSingle) e.Graphics.DrawString("My text string", myFont, Brushes.Black, _ , 100.0) e.Graphics.DrawString(nameTextBox.Text, New Font("Arial", 10), _ Brushes.Red, leftMarginSingle, currentLineSingle) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

33 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

34 PrintPageEventArgs PrintPageEventArgs argument has several useful properties MarginBounds Code as e.MarginBounds.Left e.MarginBounds.Right e.MarginBounds.Top e.MarginBounds.Bottom PageBounds PageSettings © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

35 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 SizeF Structure Use MeasureString method of the Graphics class to determine the width of a formatted string in pixels © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

36 Aligning Decimal Columns Code Example
' SizeF structure for font size info. Dim fontSizeF As New SizeF( ) ' Set X for left-aligned column. horizontalPrintLocationSingle = 200 ' Set ending position for right-aligned column. columnEndSingle = 500 ' Format the number. formattedOutputString= amountDecimal.ToString("C") ' Calculate the X position of the amount. ' Measure string in this font. fontSizeF= e.Graphics.MeasureString(formattedOutputString, _ printFont) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

37 Aligning Decimal Columns Code Example (cont)
' SizeF structure for font size info (cont). ' Subtract width of string from the column position. columnXSingle = columnEndSingle - fontSizeF.Width ' Set up the line--each element separately. e.Graphics.DrawString("The Amount = ", printFont, _ Brushes.Black, horizontalPrintLocationSingle, _ verticalPrintLocationSingle) e.Graphics.DrawString(formattedOutputString, printFont, _ Brushes.Black, columnXSingle, verticalPrintLocationSingle) ' Increment line for next line. verticalPrintLocationSingle += lineHeightSingle © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

38 Displaying a Print Preview
Add PrintPreviewDialog component to form Appears in the Component Tray Default name is fine Assign in code the same PrintDocument object you are using for printing Execute the ShowDialog method of the PrintPreviewDialog component © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

39 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

40 Printing Multiple Pages
Recall that the PrintDocument's PrintPage event fires once for each page Indicate that there are more pages to print by setting the HasMorePages property of the PrintPageEventArgs to True © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.


Download ppt "Chapter 7 Lists, Loops, and Printing"

Similar presentations


Ads by Google