Title: Chapter 7 Lists, Loops, and Printing
1Chapter 7Lists, Loops, and Printing
- Programming In
- Visual Basic.NET
2ListBoxes 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
3Types 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
4List Controls Visually
Dropdown Combo Box
Simple Combo Box
List Boxes
Dropdown List Box
5Choosing 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
6Name 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
7DropDownStyle Property
- Used to indicate the type of ComboBox
- Dropdown Combo DropDown
- Simple Combo Simple
- Dropdown List DropDownList
8Items 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
9Index 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
10SelectedIndex 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
11Items.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
12Using 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)
13Filling 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
14Filling List - Design Time
Click ellipses button to open
15Items.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)
16Items.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)
17Items.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)
18Items.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)
19Clear 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( )
20ListBox 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)
21Loops
- 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
22Do 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
23Do 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
24Do'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
25For 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
26For Next Loop General Form
- For LoopIndex InitialValue To TestValue Step
Increment - Statements to execute
- Next LoopIndex
Line continuation not shown on this slide
27Do Loops / For Next LoopsDeciding Which To Use
28Manually 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
29Making 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
30Making 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
31Sending 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
32PrintDocument 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
33The 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
35Graphics Page
36Graphics 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
37Graphics 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
38DrawString 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
39DrawString 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
40DrawString 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)
41PrintPageEventArgs Argument Properties
- MarginBounds
- Code as
- e.MarginBounds.Left
- e.MarginBounds.Right
- e.MarginBounds.Top
- e.MarginBounds.Bottom
- PageBounds
- PageSettings
42Aligning 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
43Print 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
44Printing 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
45Static 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