Chapter 7 Lists, Loops, and Printing - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

Chapter 7 Lists, Loops, and Printing

Description:

Collections are objects that have properties and methods that allow you to. Add items ... Tray, pane at bottom of Form Designer where nondisplay controls are shown ... – PowerPoint PPT presentation

Number of Views:86
Avg rating:3.0/5.0
Slides: 46
Provided by: richard542
Category:

less

Transcript and Presenter's Notes

Title: Chapter 7 Lists, Loops, and Printing


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

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
Types of List Controls
  • 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
4
List Controls Visually
Dropdown Combo Box
Simple Combo Box
List Boxes
Dropdown List Box
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
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
DropDownStyle Property
  • Used to indicate the type of ComboBox
  • Dropdown Combo DropDown
  • Simple Combo Simple
  • Dropdown List DropDownList

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
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-10)
  • 2nd item Index 1 (2-11)
  • 3rd item Index 2 (3-12)
  • So we can say that . . .
  • nth item Index n-1

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
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.Count20 BUT Inde
x 19 for the last item in list
12
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

lstSchools.Items(5) "USC" ' Next line
references the currently selected
item strSelectedFlavor lstFlavor.Items(lstFlavor
.SelectedIndex)
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
Filling List - Design Time
Click ellipses button to open
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.Ad
d(100) cboMajors.Items.Add(txtMajors.Text) 'Next
line adds new item user typed in to
combo cboGrade.Items.Add(cboGrade.Text)
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

Object.Items.Insert(IndexPosition, ItemValue)
lstDeptNums.Items.Insert(1, 100) cboGrade.Items.in
sert(0, "Freshman") cboMajors.Items.Insert(11,
txtMajors.Text)
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

Object.Items.RemoveAt(IndexPosition)
lstDeptNums.Items.RemoveAt(1) ' Next line
removes the currently selected item cboGrade.Items
.RemoveAt(cboGrade.SelectedIndex)
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

Object.Items.Remove(TextString)
lstDeptNums.Items.Remove("USC") ' Next line
removes the currently selected item cboGrade.Items
.Remove(cboGrade.Text)
19
Clear Method
  • Empty the entire list from list box or combo box,
    remove all the items
  • General Form
  • Examples

Object.Items.Clear( )
lstDeptNums.Items.Clear( ) cboGrade.Items.Clear( )
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
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
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
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
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
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
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
Do Loops / For Next LoopsDeciding Which To Use
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
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
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
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
PrintDocument Control
  • Add PrintDocument controlto 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
The Printing Saga in .NETSetting 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
Private Sub btnPrint_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles btnPrint.Click prtDocument.Print()
will print only blank a blanck page
Unless there is a prtDocument_PrintPage(
Which is is fired once for each page to be
printed End Sub
argument e as System.Drawing.Printing.PrintPageEv
entArgs useful for handling page margins and
sending strings of text to the page
35
Graphics Page
36
Graphics Page
X ?
Y ?
  • Set up in memory and then send thepage 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

37
Graphics Page
  • Set up in memory and then send thepage 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

38
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

39
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

40
DrawString Method (cont.)
General Form
DrawString(StringToPrint, Font, Brush,
Xcoordinate, Ycoordinate)
Examples
e.Graphics.DrawString(strLine, fntFont,
Brushes.Black, sngX, sngY) e.Graphics.DrawStri
ng("My String", fntMyFnt, Brushes.Red, 100.0,
100.0) e.Graphics.DrawString(txtName.Text, New
Font("Arial", 10), Brushes.Black, sngLeftMargin,
sngCurrentLine)
41
PrintPageEventArgs Argument Properties
  • MarginBounds
  • Code as
  • e.MarginBounds.Left
  • e.MarginBounds.Right
  • e.MarginBounds.Top
  • e.MarginBounds.Bottom
  • PageBounds
  • PageSettings

42
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

43
Print Preview
  • Add PrintPreviewDialogcontrol 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

44
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

45
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
Write a Comment
User Comments (0)
About PowerShow.com