McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Lists, Loops, and Printing Chapter 7.

Slides:



Advertisements
Similar presentations
Chapter 7 Lists, Loops, and Printing Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Advertisements

Programming with Microsoft Visual Basic th Edition
Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Microsoft Visual Basic: Reloaded Chapter Six Repeating Program Instructions.
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
Chapter 6 Multiform Projects Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Do/Loops A loop repeats 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 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Chapter 7 Lists, Loops, and Printing
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
Chapter 7 Loops and Printing Programming In Visual Basic.NET.
1 Windows Printing. 2 Objectives You will be able to Output text and graphics to a printer. Print multipage documents. Use the standard Windows print.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS.
Chapter 8: String Manipulation
CHAPTER SIX Loop Structures.
CHAPTER 6 Loop Structures.
Lesson 1 – Microsoft Excel The goal of this lesson is for students to successfully explore and describe the Excel window and to create a new worksheet.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 26 – CheckWriter Application Introducing Graphics.
Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to accompany prepared by James T. Perry University of San.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
5-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
 What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures.
110 M - 1 ListBoxes & ComboBoxes Provide a list for the user to select from Various styles, choose based on –Amount of data to be displayed –Space available.
Chapter 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Seven More on the Repetition Structure.
Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops.
5-1 Chapter 5 The Repetition Process in VB.NET. 5-2 Learning Objectives Understand the importance of the repetition process in programming. Describe the.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved CheckWriter Application Introducing Graphics and Printing.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 6 Multiform Projects.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 1 Unit 8 List Boxes and the Do While Looping Structure.
Visual Basic Programming Chapter Six Notes Repetition and the Do Statement ADDING ICONS TO YOUR FORM –It is possible to add an ______________ to your title.
7-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Chapter 6: The Repetition Structure
Tutorial 6 The Repetition Structure
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Six The Do Loop and List Boxes.
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. WORD 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 15 Advanced Tables.
Chapter 7 Lists, Loops, and Printing Programming In Visual Basic.NET.
Introduction to Problem Solving and Control Statements.
Chapter 7 P 1 Lists and Loops List boxes and combo boxes List box - list of items - useful for a list which does not change Combo box - list of items -
Chapter 7 - Lists, loops and printing w List boxes and combo boxes several types can add items at design time or during run time user select from predefined.
7-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
For…Next Loops, Checked List Boxes, and Combo Boxes Chapter 5.
 A ListBox control displays a list of items and allows the user to select one or more  Drag from Toolbox to create this control on a form.
Chapter 5 Menus, Sub Procedures and Sub Functions Programming In Visual Basic.NET.
Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed.
Visual Basic.NET BASICS Lesson 11 List Boxes, For Next Loops, and Label Settings.
List Boxes and Combo Boxes Provides a list of items to select from Various styles — choose based on Space available Need to select from an existing list.
Unit 6 Repetition Processing Instructor: Brent Presley.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Input Boxes, List Boxes, and Loops Chapter 5. 2 Input Boxes Method for getting user’s attention to obtain input. InputBox() for obtaining input MessageBox()
Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code (loop body) that may be executed several times. Fixed-count (definite) loops repeat.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 1 Unit 9 Do Until and For… Next Loops Chapter 5 Lists,
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 6 Looping and Multiple Forms.
COMPUTER PROGRAMMING I Apply Procedures to Develop List Box and Combo Box Objects.
Microsoft Visual Basic 2008: Reloaded Third Edition
Using Procedures and Exception Handling
Repeating Program Instructions
Microsoft Visual Basic 2005: Reloaded Second Edition
CIS 16 Application Development Programming with Visual Basic
Introduction to Problem Solving and Control Statements
Repetition and Multiple Forms
Chapter 7 Lists, Loops, and Printing
Presentation transcript:

McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Lists, Loops, and Printing Chapter 7

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 7-2 Objectives (1 of 2) Create and use 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.

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 7-3 Objectives (2 of 2) Display a selected item from a list. Use Do/Loops and For/Next statements to iterate through a loop. Terminate a loop with the Exit statement. Skip to the next iteration of a loop by using the Continue statement. Send information to the printer or the Print Preview window using the PrintDocument class.

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

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 7-5 ListBoxes and ComboBoxes (1 of 2) Have most of the same properties and operate in a similar fashion An exception is that a combo box control has a DropDownStyle property 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

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 7-6 List Boxes and Combo Boxes (2 of2) Various Styles of List and Combo boxes

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 7-7 The Items Collection List of items in a ListBox or ComboBox is a collection. VB Collections are objects that have properties and methods that allow Adding items Removing items Referring to individual elements Counting items Clearing the collection

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 7-8 Filling a List/Using the Properties Window Design time in Properties window 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

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 7-9 Using the 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)

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

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

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved The Items.Count Property to determine number of items in the list Remember: Items.Count is always one more than the highest possible SelectedIndex, because indexes begin with 0 For example, if there are five items in a list: Items.Count = 5AND Highest Index = 4

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved 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" MajorLabel.Text = MajorsComboBox.Items(IndexInteger) SelectedMajorLabel.Text = MajorsComboBox.Items(MajorsComboBox.SelectedIndex) SelectedMajorLabel.Text = MajorsComboBox.Text

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved Removing an Item from a List Use the Items.RemoveAt method to remove an item by index from the list and the Items.Remove method to remove by specifying the text. General Form Examples NamesListBox.Items.RemoveAt(0) ' Remove the item in position IndexInteger. SchoolsComboBox.Items.RemoveAt(IndexInt eger) CoffeeComboBox.Items.RemoveAt(CoffeeCo mboBox.SelectedIndex) Object.Items.RemoveAt(IndexPosition)

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved The Items.Remove Method Use the Items.Remove method to remove an item by specifying the text. General Form Examples NamesListBox.Items.Remove("My School " ) SchoolsComboBox.Items.Remove(SchoolTextBox.Text) ' Next line removes the currently selected item. CoffeeComboBox.Items.Remove(CoffeeComboBox.Text) Object.Items.Remove(TextString)

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

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved List Box and Combo Box Events In the Editor window, select the control name in the Class Name list (at the top-left of the window), drop down the Method Name list, and select the event for which you want to write code or double-click the event name in the Properties window after clicking the Events button. The Editor will create the procedure header for you. TextChanged Event Occurs when user types text into combo box List box does not have TextChanged Event. Enter Event (control receives focus) — an Enter event fires when a user tabs from control to control. Leave Event (control loses focus) — a Leave event triggers as user tabs between controls.

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved Do/Loops A loop repeats 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 A Do/Loop terminates based on a specified condition. Execution of the loop continues while a condition is True or until a condition is True. The condition can be placed at the top or the bottom of the loop.

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved The Do and Loop Statements — General Form Do {While |Until} condition ' Statements in loop. Loop --OR-- Do ' Statements in loop. Loop {While | Until} condition Top of Loop Condition, Pretest/Entry test Bottom of Loop Condition, Posttest/ Exit

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

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved Pretest vs. Posttest Diagram

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved The Boolean Data Type Revisited Can help when searching a list for a specific value Boolean variable is always in one of two states: True or False. When a particular situation occurs, set Boolean variable to True. Use a loop to check for True Many programmers refer to Boolean variables as switches or flags. Switches have two states — on or off. Flags are considered either up or down.

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved For/Next Loops Used to repeat statements in a loop a specific number of times Uses a numeric counter variable, called Loop Index, which is tested to determine the number of times the statements inside the loop will execute 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.

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved The For and Next Statements — General Form For LoopIndex [As DataType] = InitialValue To TestValue [Step Increment] ' Statements in loop. Next [LoopIndex] A For/Next loop can handle all three elements of a counter- controlled loop. Initialize the counter. Increment the counter. Test the counter to determine when it is time to terminate the loop.

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved For/Next Loop Diagram

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved Exiting Loops In some situations, you may need to exit the loop prematurely. Click on the form’s close box or use the VB menu bar or toolbar to stop the program; or Ctrl+Break. Use the Exit For statement inside the loop structure. Generally, the Exit For statement is part of an If statement.

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved 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. 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 to make a single item in a list box appear selected.

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved Sending Information to the Printer Components appear in the Printing tab of the toolbox. Most professional programmers use a separate utility program to format printer reports. Several companies sell utilities that do a nice job designing and printing reports. VB Professional Edition and Enterprise Edition include Crystal Reports for creating reports from database files.

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved The PrintDocument Component Appears in the Component Tray Execute the Print method to start printing. The code belongs in the Click event procedure for the Print button or menu item that can be selected to begin printing.

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

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved The Graphics Page Set up graphics page in memory and then the 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. X coordinate is the horizontal distance from across the page; the Y coordinate is the vertical distance from the top of the page.

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved 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 Arguments for the DrawString method include: What to print What font and color to print in Where to print Set up the Font and X/Y coordinates to be used before executing the DrawString method.

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

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved Setting the X and Y Coordinates For each print line, specify X and Y coordinates. Create variables declared as Single data type to set the X and Y values. Dim HorizontalPrintLocationSingle As Single Dim VerticalPrintLocationSingle As Single

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved PrintPageEventArgs PrintPageEventArgs argument has several useful properties that are used to determine the present settings. – MarginBounds – PageBounds – PageSettings

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved Printing the Contents of a List Box Techniques for printing, a loop, and the list box properties can be combined to send the contents of a list box to the printer Use the Items.Count property as the number of iterations to make. Items collection allows printing out the actual values from the list.

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved Aligning Decimal Columns It is important to align the decimal points of numeric data. Proportional fonts make aligning decimal points tricky. Format each number to be printed and measure the length of the formatted string. Declare an object as a SizeF Structure which has a Width property. Use MeasureString method of the Graphics class to determine the width of a formatted string in pixels.

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved Aligning Decimal Columns Code Example (1 of 2) ' 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)

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved Aligning Decimal Columns Code Example (2 of 2) ' 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

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved Displaying a Print Preview The PrintPreviewDialog component is the key to 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.

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved PrintPreviewDialog Component

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved The Using Block System resources such as fonts can be access inside a Using block. Variables that are declared in a Using block are only accessible within that block. The advantage of declaring a variable inside a Using block is that system resources are released as soon as the block terminates.

McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved Printing Multiple Pages The PrintDocument’s PrintPage event fires once for each page. Set the HasMorePages property of the PrintPageEventArgs argument to True to print more than one page.

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