Top Banner
Chapter 7 Lists, Loops, and Printing Programming in C# .NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
41

Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

Jan 12, 2016

Download

Documents

Julius Nash
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

Chapter 7Lists, Loops, and

Printing

Programming in C# .NET

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

Page 2: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 2

Objectives

• Create and use the list boxes and combo boxes• Differentiate among the available types of combo

boxes• Enter items into list boxes using the Items

collection in the Properties window• Add and remove items in a list at run time• Determine which item in a list is selected• Use the Items.Count property to determine the

number of items in a list

Page 3: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 3

Objectives cont.

• Display a selected item from a list

• Use do, while, and for loops to repeatedly execute a series of statements

• Send information to the printer or the Print Preview window using the PrintDocument class

Page 4: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 4

List Boxes and Combo Boxes

• Types of list boxes and combo boxes– Simple list boxes

– Simple combo boxes

– Drop-down combo boxes

– Drop-down lists

• List boxes and combo boxes have many common properties and operate similarly

• Combo box control has a DropDownStyle property to determine if list box has a text box for user entry and whether list will drop down

Page 5: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 5

List Boxes and Combo Boxes cont.

• Scroll bars are added automatically if the box is too small to display all items in list

• Select list style based on space available and how box should operate

• List box displays Name property in control• Combo box displays Text property in control• List box Text property available only at run

time

Page 6: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 6

The Items Collection

• List of items in a list box or combo box is a collection

• Collections are objects with properties and methods

• Items collection used to add items, remove items, refer to items, count the items, and clear the list

• Refer to items in collection by an index which starts at zero

• Refer to first item in the Items collection as Items[0]

Page 7: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 7

Filling a List

• Use Items property at design time if list contents are known and will not change

• Click on ellipses button next to Items property to open String Collection Editor

• Use String Collection Editor to add items to list

• To add items during execution, use the Items.Add or Items.Insert method

Page 8: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 8

Using the Items.Add Method

• General FormObject.Items.Add(ItemValue);

• New item added to end of the list• Set the control’s Sorted property to true to place item

alphabetically in list• Must use the Add method to add value user types in

text box of combo box to the list• Example:

coffeeComboBox.Items.Add(coffeeComboBox.Text);

Page 9: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 9

Using the Items.Insert Method

• Choose location for a new item in the list by specifying the index position

• General Form Object.Items.Insert(IndexPosition, ItemValue);

• Index is zero based• Do not set the list control’s Sorted property to

true if using the Insert method• Example: schoolsListBox.Items.Insert(0,”Harvard”);

Page 10: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 10

The SelectedIndex Property

• SelectedIndex property contains index number of item the user selects (highlights)

• If no list item is selected, SelectedIndex is set to negative 1 (-1)

• Example: coffeeTypesListBox.SelectedIndex = 3; //Select fourth item in list

Page 11: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 11

The Items.Count Property

• Count property of Items collection contains number of items in the list

• Items.Count is always one more than the highest possible SelectedIndex

• Example:intTotalItems =

itemsListBox.Items.Count;

Page 12: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 12

Referencing the Items Collection

• Specify an element by including an index• General Form Object.Items[IndexPosition]

• Highest index is Items.Count – 1

• Combine Items property and SelectedIndex property to refer to currently selected element

• Example: strSelectedFlavor = FlavorListBox.Items[FlavorListBox.SelectedIndex];

Page 13: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 13

Referencing the Items Collection cont.

• Retrieve selected list item with Text property of control

• Example:selectedMajorLabel.Text = majorsComboBox.Text;

• If you assign a value to a particular item, you replace the previous contents of that position

Page 14: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 14

Removing an Item from a List

• Specify the index or text of item to remove it from a list

• Use Items.RemoveAt method to remove by index• General Form

Object.Items.RemoveAt(IndexPosition);

• Use Items.Remove method to remove by text• General Form

Object.Items.Remove(TextString);

Page 15: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 15

Clearing a List

• Use Items.Clear method to empty a combo box or list box

• General Form Object.Items.Clear();

• Example: schoolsListBox.Items.Clear();

Page 16: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 16

List Box and Combo Box Events

• TextChanged event occurs as user types text into text box portion of combo box

• Enter event occurs when a control receives the focus

• Leave event occurs when the control loses focus

Page 17: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 17

The while and do/while Loops

• Looping is the process of repeating a series of instructions

• The group of repeated instructions is the loop• An iteration is a single execution of the

statement(s) in the loop• A while or do/while loop terminates based on a

condition• Use while or do/while loops when you don’t know

how many iterations are needed

Page 18: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 18

The while and do/while Loops cont.

• General Formwhile (Condition){

//Statements in loop}

ordo{

//Statements in loop} while (Condition);

Page 19: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 19

The while and do/while Loops cont.

• Testing for completion at top of loop is a pretest• If terminating condition is true, statements in loop

may never execute• Example:

intTotal = 0;

while (intTotal != 0)

{

//Statements in loop

}

Page 20: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 20

The while and do/while Loops cont.

• Testing for completion at bottom of loop is called a posttest

• Statements inside loop will always execute at least once

• Example:intTotal = 0;do{

//Statements in loop} while (intTotal != 0);

Page 21: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 21

The bool Data Type Revisited

• Set a boolean variable to true when a specific circumstance occurs and write a loop to end when the variable is true

• Using a boolean variable– Declare a variable and set its initial value

– When particular situation occurs, set variable to true

– Write a loop condition to check for true

• A boolean variable is always one of two states – true or false

Page 22: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 22

for Loops

• Use a for loop to repeat statements in a loop a specific number of times

• for loop has three parts– Initialization– The condition– The action to occur

• Use a semicolon to separate the parts of the for statement

• Use a loop index to count iterations

Page 23: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 23

for Loops cont.

• General Form for (initialization [,additional initialization]; condition;

action [,additional action])

{

//(Body of loop)

}

• Examples:for (int Index = 2; intIndex <= 100; intIndex += 2)

for (intCount = intStart; intCount < intEnding; intCount += intIncrement)

Page 24: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 24

for Loops cont.

• You can decrement the counter and test for the lower bound in the condition

• Statements in body of loop will not execute if the final value is reached before entry into the loop

• Some code can get into an endless loop that is impossible to exit on its own– Click on form’s close box

– Use Visual Studio IDE menu or toolbar to stop program

– Use the C# break statement

Page 25: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 25

Making Entries Appear Selected

• Use the SelectAll method to select text in a text box in the text box’s Enter event handler

• Select a single item in a list box with the SelectedIndex property

• Select the matching entry as user types in text box by coding the TextChanged event handler

Page 26: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 26

Sending Information to the Printer

• Use the .NET PrintDocument and PrintPreviewDialog classes to send output to printer and preview on the screen

• C# Profesional Edition and Enterprise Edition include Crystal Reports for creating reports from database files

Page 27: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 27

The PrintDocument Control

• Set up output for printer using the methods and events of the PrintDocument control

• PrintDocument control appears in component tray below the form

• Use the Print method of PrintDocument control to start printing

Page 28: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 28

Setting Up the Print Output

• Logic for printing is coded in the PrintPage event-handling method of the PrintDocument control

• PrintPage event fired for each page to be printed in process known as callback

• PrintDocument object activated when its Print method is issued

Page 29: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 29

The Graphics Page

• Set up a graphics page in memory

• Page is sent to the printer

• Can contain strings of text and graphic elements

• Must specify exact location on graphics page for each element to print

Page 30: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 30

Using the DrawString Method

• Use DrawString method to send line of text to the graphics page

• General FormDrawString(StringToPrint, Font, Brush, Xcoordinate, Ycoordinate)

• Examples:e.Graphics.DrawString(strPrintLine, printFont, Brushes.Black,

fltPrintX, fltPrintY);

e.Graphics.DrawString(“My text string”, myFont, Brushes.Black,

100.0f, 100.0f);

Page 31: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 31

Setting the X and Y Coordinates

• Create float data type variables for these values

• PrintPageEventArgs argument properties:– MarginBounds– PageBounds– PageSettings

• To send multiple lines to the print page, increment the Y coordinate

Page 32: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 32

Printing the Contents of a List Box

//Print out all items in the coffeeComboBox list

for (int intListIndex = 0; intListIndex < coffeeComboBox.Items.Count – 1; intListIndex++)

{

//Set up a line

strPrintLine = coffeeComboBox.Items[intListIndex].ToString();

//Send the line to the graphics page object

e.Graphics.DrawString(strPrintLine, printFont, Brushes.Black, fltPrintX, fltPrintY);

//Increment the Y position for the next line

fltPrintY += fltLineHeight;

}

Page 33: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 33

Printing cont.

• Use the Text property of a list box or combo box to print the selected item

• To align decimal points in output to a printer, format each number and measure the length of the formatted string

• Add a PrintPreviewDialog control to print preview– Uses the same PrintDocument control that was declared

for printer output– Assign the PrintDocument to the Document property

of the PrintPreviewDialog control

Page 34: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 34

Printing Multiple Pages

• Indicate there are more pages to print by setting the HasMorePages property of the PrintPageEventArgs argument to true

• To print a page number on each page, declare a class-level variable to hold the page counter

Page 35: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 35

Your Hands-On Programming Example

Create a project for R ‘n R – for Reading ‘n Refreshment that contains a drop-down combo box of the coffee flavors and a list box of the syrup flavors. Adjust the size of the boxes as needed when you test the project. The controls should have labels above them with the words Coffee and Syrup. Enter the initial values for the syrup flavors and coffee flavors in the Properties window. Set the Sorted property of both lists to true. The user will be able to add more coffee flavors to the list at run time.

Page 36: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 36

Your Hands-On Programming Example cont.

Include one menu item to print all the flavors and another to print only a selected item from each list. Then include submenus for each of the print options to allow the user to send the output to the printer or the Print Preview window. These print commands belong on the File menu, along with the Exit command. Use a separator bar between the Prints and the Exit.

Page 37: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 37

Your Hands-On Programming Example cont.

Include an Edit menu with commands to Add coffee flavor, Remove coffee flavor, Clear coffee list, and Display coffee count.

Add an About form into your project and add a Help menu with an About command.

After you have completed the project, try using different styles for the combo box and rerun the project. As an added challenge, modify the add coffee flavor routine so that no duplicates are allowed.

Page 38: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 38

Summary

• List boxes and combo boxes hold lists of values. Three combo box styles are simple, drop-down, and drop-down list.

• Size of list box or combo box determined at design time. Scroll bars added automatically.

• Values for items in list stored in Items property which is a collection. Items entered with Items property or using Items.Add or Items.Insert methods.

• The SelectedIndex property can be used to select an item on a list or determine what item was selected.

Page 39: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 39

Summary cont.

• The Items.Count property holds the number of elements in a list.

• The Items property holds all elements in a list and are referenced using an index.

• The Items.Remove and Items.RemoveAt methods remove one element from the list.

• The Items.Clear method is used to clear all contents of a list at once.

• Code can be written for several events of list boxes and combo boxes. Combo boxes have a TextChanged event; both combo and list boxes have Enter and Leave events.

Page 40: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 40

Summary cont.

• A loop allows one or more statements to be repeated.

• The while loop has the condition at the top of the loop (pretest) and do/while loop has condition at the bottom (posttest).

• A loop can be used to locate a selected item in a combo box.

• A for loop contains an initialization section, a condition section, and an action.

Page 41: Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.7- 41

Summary cont.

• The PrintDocument and PrintPreviewDialog controls are used to send program output to the printer or the screen

• The Print method of the PrintDialog control begins a print operation. Printing logic is placed in the PrintPage event handler.

• The page to print or display is a graphics object. Use DrawString method to set up the page.

• Aligning columns of numbers is difficult using proportional fonts.