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

Slides:



Advertisements
Similar presentations
Lists, Loops, Validation, and More
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Chapter 7 Lists, Loops, and Printing Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Chapter 6: The Repetition Structure
Programming with Microsoft Visual Basic 2008 Fourth Edition
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.
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
Programming Based on Events
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
Chapter 7 Loops and Printing Programming In Visual Basic.NET.
Chapter 8 Using Repetition with Loops and Lists. Class 8: Loops and Lists Write Do loops to execute statements repeatedly Write For loops to execute statements.
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.
WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS.
© 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.
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.
ListBox, ComboBox, Menu Chapter 5.4, ComboBox Control: Properties & Methods u Combines TextBox features with a short drop- down list  cboOne.AddItem(string)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 – Graphical User Interfaces Part 2 Outline.
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 7 Loops Programming In Visual Basic.NET Prepared by Johnny Tsui,
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.
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.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Lists, Loops, and Printing Chapter 7.
7-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al- ajmi Chapter 3 Some Visual Basic Controls and Events Visual Basic. NET.
 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.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
IMS 3253: Controls 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Check Boxes Radio Buttons Date Time Picker Masked.
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,
COMPUTER PROGRAMMING I Apply Procedures to Develop List Box and Combo Box Objects.
Microsoft Visual Basic 2008: Reloaded Third Edition
Apply Procedures to Develop Menus, List Box and Combo Box Objects
Apply Procedures to Develop Menus, List Box and Combo Box Objects
Repeating Program Instructions
Microsoft Visual Basic 2005: Reloaded Second Edition
CIS16 Application Development and Programming using Visual Basic.net
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:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved Aligning Decimal Columns (code example p ) 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

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

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

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