Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 7 Lists, Loops, and Printing Programming In Visual Basic.NET."— Presentation transcript:

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

2 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 2 ListBoxes & ComboBoxes Provide a list for the user to select from Various styles, choose based on –Amount of data to be displayed –Space available –User's ability to add to the list

3 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 3 ListBox tool –1st prefix –Simple List Box with/without scroll bars ComboBox tool –cbo prefix –List may allow for user to add new items –List may "drop down" to display items in list CheckedListBox not covered in this text Types of List Controls

4 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 4 List Controls Visually List Boxes Dropdown Combo Box Simple Combo Box Dropdown List Box

5 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 5 Choosing List Type Many items, little space –Dropdown Combo Box –Dropdown List Few items –List Box User allowed to add to the list –Simple Combo Box –Dropdown Combo Box

6 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 6 Name and Text Properties ListBoxes –Name displayed at Design Time –Text accessible only at Run Time ComboBoxes –Text displayed and accessible at Design Time –Text also accessible at Run Time –If user enters new data in ComboBox then VB stores it temporarily in the Text property

7 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 7 DropDownStyle Property Used to indicate the type of ComboBox –Dropdown Combo = DropDown –Simple Combo = Simple –Dropdown List = DropDownList

8 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 8 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 items –Count items –Clear the collection

9 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 9 Index Property Zero based value used to reference individual items in the collection Position of an item in the list –1st item Index = 0 (1-1=0) –2nd itemIndex = 1 (2-1=1) –3rd item Index = 2 (3-1=2) So we can say that... –nth itemIndex = n-1

10 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 10 SelectedIndex Property Use to identify currently selected item in the list If no item is selected ListIndex = -1 Use to select an item in the list

11 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 11 Items.Count Property Use to determine number of items in the list Remember: Items.Count is the actual number of items in the list BUT the SelectedIndex property and Index will be 1 less. For example, it there are 20 items in the list: Items.Count=20BUT Index =19 for the last item in list

12 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 12 lstSchools.Items(5) = "USC" ' Next line references the currently selected item strSelectedFlavor = lstFlavor.Items(lstFlavor.SelectedIndex) Using 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 1st item in the list is index position zero

13 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 13 Filling a List Design time in properties window –Items property –Click on ellipses to open String Collection Editor –Separate items with ENTER key Run time methods –Items.Add OR –Items.Insert

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

15 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 15 Items.Add Method Use to add new items to the list at run time General Form Examples Object.Items.Add(ItemValue) lstNames.Items.Add("Brandon") lstDeptNums.Items.Add(100) cboMajors.Items.Add(txtMajors.Text) 'Next line adds new item user typed in to combo cboGrade.Items.Add(cboGrade.Text)

16 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 16 Items.Insert Method Use to add new items to the list at run time at a specific location(index) in the collection General Form Examples lstDeptNums.Items.Insert(1, 100) cboGrade.Items.insert(0, "Freshman") cboMajors.Items.Insert(11, txtMajors.Text) Object.Items.Insert(IndexPosition, ItemValue)

17 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 17 Items.RemoveAt Method Use to specific remove items from the list at run time by specifying the index of the item General Form Examples lstDeptNums.Items.RemoveAt(1) ' Next line removes the currently selected item cboGrade.Items.RemoveAt(cboGrade.SelectedIndex) Object.Items.RemoveAt(IndexPosition)

18 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 18 Items.Remove Method Use to specific remove items from the list at run time by specifying the Text of the item General Form Examples lstDeptNums.Items.Remove("USC") ' Next line removes the currently selected item cboGrade.Items.Remove(cboGrade.Text) Object.Items.Remove(TextString)

19 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 19 Clear Method Empty the entire list from list box or combo box, remove all the items General Form Examples lstDeptNums.Items.Clear( ) cboGrade.Items.Clear( ) Object.Items.Clear( )

20 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 20 ListBox and ComboBox Events TextChanged Event –Occurs when user types text into Combo –ListBox does not have TextChanged Event SelectedIndexChanged Event Enter Event (receive focus) Leave Event (lose focus)

21 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 21 Loops Repeating a series of instructions Each repetition is called an iteration Types of Loops –Do Use when the number of iterations is unknown –For Next Use when the number of iterations known

22 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 22 Do Loops Ends based on a condition you specify, either –Loop While a condition is True –Loop Until a condition becomes True Condition can be located at –Top of Loop, Pretest –Bottom of Loop, Posttest

23 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 23 Do Loop General Form Do {While |Until} condition Statements to execute Loop OR Do Statements to execute Loop {While | Until} condition Top of Loop Condition, Pretest Bottom of Loop Condition, Posttest

24 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 24 Do's - When Evaluated Top Evaluation, not guaranteed to run once since evaluated BEFORE running Do While … Loop Do Until … Loop Bottom Evaluation, will always run at least one time Do … Loop While Do … Loop Until

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

26 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 26 For Next Loop General Form For LoopIndex = InitialValue To TestValue [Step Increment] Statements to execute Next [LoopIndex] Line continuation not shown on this slide

27 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 27 Do Loops / For Next Loops: Deciding Which To Use Is the number of repetitions known? Is the expression initially true? Must the loop execute once? Top Eval Do Loop Bottom Eval Do LoopFor Next Loop Do Until LoopDo While Loop NO YES NO YES

28 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 28 Manually 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 is part of an If statement

29 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 29 Making Entries Appear Selected In Windows, when a user tabs into a text box or one of the list controls the data in the control is usually selected Also, when setting the focus in code to a text box or list it is customary to select all the data, for example on invalid data during validation

30 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 30 Making Entries Appear Selected (cont.) TextBox –Add the SelectAll method to the TextBoxName_Enter event ListBox or ComboBox –Set the SelectedIndex property –Examine the code listing for p 308 which selects matching entries from the list as the user types characters

31 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 31 Sending Data to the Printer PrintDocument class PrintPreviewDialog class Consider using a 3rd party software package for printing Crystal Reports is a utility packaged with the VB Professional and Enterprise Editions for creating reports from database files

32 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 32 PrintDocument Control Add PrintDocument control to form –Appears in the Component Tray, pane at bottom of Form Designer where nondisplay controls are shown –Name using prt prefix Execute the Print method to start printing Logic for actual printing belongs in the PrintDocument's PrintPage event procedure

33 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 33 Setting Up the Print Output PrintPage event is fired once for each page to be printed 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

34 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 34 Graphics Page Set up in memory and then send the page to the printer Can contain strings of text and graphic elements Specify the exact X and Y coordinates, in pixels, of each element to be printed on the page X = horizontal distance from left edge of paper Y = vertical distance from top edge of paper

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

36 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 36 DrawString Method (cont.) General Form Examples DrawString(StringToPrint, Font, Brush, Xcoordinate, Ycoordinate) e.Graphics.DrawString(strLine, fntFont, Brushes.Black, sngX, sngY) e.Graphics.DrawString("My String", fntMyFnt, Brushes.Red, 100.0, 100.0) e.Graphics.DrawString(txtName.Text, New Font("Arial", 10), Brushes.Black, sngLeftMargin, sngCurrentLine)

37 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 37 PrintPageEventArgs Argument Properties MarginBounds –Code as e.MarginBounds.Left e.MarginBounds.Right e.MarginBounds.Top e.MarginBounds.Bottom PageBounds PageSettings

38 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 38 Aligning Decimal Columns (code example p 313-314) 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 to determine the width of a formatted string in pixels

39 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 39 Print Preview Add PrintPreviewDialog control to form –Appears in the Component Tray, pane at bottom of Form Designer where nondisplay controls are shown –Name using ppd prefix Assign the same PrintDocument object you are using for printing to it in code Execute the ShowDialog method of the PrintPreviewDialog control

40 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 40 Static Variables Local variable that 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

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


Download ppt "Chapter 7 Lists, Loops, and Printing Programming In Visual Basic.NET."

Similar presentations


Ads by Google