• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Chapter 07
 

Chapter 07

on

  • 1,046 views

 

Statistics

Views

Total Views
1,046
Views on SlideShare
1,044
Embed Views
2

Actions

Likes
0
Downloads
10
Comments
0

2 Embeds 2

http://www.slideshare.net 1
http://tyoastos.pbworks.com 1

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Brief description on how to navigate within this presentation (ppt) The first time a Key Term from the chapter is used in the ppt it will display in blue Gold colored text boxes display coding examples Slides will be numbered (# of #) when multiple slides on same topic (Slide title) Speaker notes are included where appropriate for slides (*)Denotes either a comment for page reference to textbook or slide reference in ppt
  • Often there is a want to offer the user a list of items from which to choose-the Windows ListBox and ComboBox controls (key term) can be used to display lists on a form Items may be added to a list during design time, run time, or perhaps a combination of both
  • When adding a list control to a form, choose the style according to the space that is available and the box is to operate At design time, the behavior of list boxes and combo boxes differs—for list boxes VB displays the Name property in the control; for combo boxes the Text property displays which is blank by default Combo boxes have a Text property which can be set at design time; List boxes also have a Text property it can only be accessed at run time
  • Items in a collection can be referred to by an index, which is zero based—for example, if a collection holds 10 items, the indexes to refer to the items range from 0 to 9; the first tiem in the Items collection would be Item 0
  • Several methods can be used to fill the Items collection of a List box and Combo box. If the list contents are known at design time and the list never changes, it can be defined in the Properties window If items must be added to the list during program execution the Items.Add or Items.Insert method (key term) in an event procedure is used
  • Items to add can be a variable, a constant, the contents of the Text box at the top of a Combo box, or the Text property of another control
  • The index position is zero based-to insert a new item in the first position, use index position = 0
  • The SelectedIndex Property is used when a project is running and the user selects (highlights) an item from the list
  • Used to display one item from a list-refer to one element of the Items collection Specify which element is wanted by including an index This technique can be useful if needed to display a list item in a label or on another form
  • In addition to removing individual items at run time, items can also be cleared from a list
  • The computer is capable of repeating a group of instructions many times without calling the procedure for each new set of data—the process is called looping
  • *Next slide shows examples of prettest and posttest loops
  • Boolean variables can be very useful when setting and testing conditions for a loop Can be used to search through a list for a specific value Three step process-dimension a variable; set its initial value and then set a variable to True
  • The For/Next loop uses the For and Next statements as well as a counter variable called the loop index
  • The next slide shows a For/Next loop diagram
  • If in an endless loop that can be caused by changing the value of a loop index variable the program execution may need to be broken manually
  • There are several techniques used to make the text in a text box or list appear selected
  • .NET PrintDocument and PrintPreviewDialog components can be used to produce output for the printer and also to preview the output on the screen
  • The logic for the actual printing belongs in the PrintDocument’s PrintPage event procedure (Key term)
  • Exact locations needs to be specified for each element of the graphics pages that is to be printed You can specify the upper-left corner of any element by giving its X and Y coordinates, or by using a Point structure or a Rectangle structure You can use multiple PrintDocument objects if you have more than one type of output or report Each PrintDocument has its own PrintPage event Code the graphics commands to precisely print the page in each document’s PrintPage event procedure
  • The next slide displays the General Form and examples of the DrawString Method
  • If printing a selected Item from a List—the Text property holds the selected item and can be used to print that selected item
  • The next 2 slides display an example of sample code for Aligning Decimal Columns
  • A really great new feature of the new VB.NET printing model is print preview It’s easy to view the printer’s output on the screen and then decide to print or cancel *The next slide displays the PrintPreview Dialog Component

Chapter 07 Chapter 07 Presentation Transcript

  • aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf
  • Lists, Loops, and Printing Chapter 7 McGraw-Hill © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
  • 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
  • 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
  • ListBoxes and ComboBoxes (1 of 2)
    • Have most of the same properties and operating in a similar fashion
      • An exception is that a combo box control has a DropDown Style 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
  • List Boxes and Combo Boxes (2 of2) Various Styles of List and Combo boxes
  • 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
  • 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
  • Filling a List - Design Time Click ellipses button to open
  • Using the Items.Add Method
    • Use to add new items to the list at run time
    • General Form
    • Examples
    Object . Items . Add( ItemValue ) Me.schoolsListBox.Items.Add("Harvard " ) Me.schoolsListBox.Items.Add( " Stanford " ) Me.schoolsListBox.Items.Add(schoolsTextBox.Text) Me.majorsComboBox.Items.Add(majorsComboBox.Text) Me.majorsComboBox.Items.Add(majorString)
  • 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
    Me.schoolsListBox.Items.Insert(0, " Harvard " ) Me.majorsComboBox.Items.Insert(1, Me.majorsComboBox.Text) Object . Items . Insert( IndexPosition , ItemValue )
  • 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
  • The Items . Count Property
    • Use to determine number of items in the list
      • Remember:
      • Items . Count is always one more than the highest possible Selected Index because indexes begin with 0
      • For example, if there are five items in a list:
          • Items.Count = 5 AND
          • Highest Index = 4
  • 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
    Me.schoolsListBox.Items(5) = "University of California" ' Next line references the currently selected item. selectedFlavorString = flavorListBox.Items(flavorListBox.Selected Index).ToString( )
  • 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
    Me.namesListBox . Items . RemoveAt(0) ‘ Remove the item in position indexInteger. Me.schoolsComboBox.Items.RemoveAt(indexInteger) ‘ Remove the currently selected item. Object . Items . RemoveAt( IndexPosition )
  • 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 )
  • Clearing a List
    • Use the Items.Clear method to clear all items and empty a combo box or list box
    • General Form
    • Examples
    Me.schoolsListBox . Items . Clear( ) Me.majorsComboBox . Items . Clear( ) Object . Items . Clear( )
  • 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 a user tabs between controls
  • 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
  • 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 Bottom of Loop Condition, Posttest
  • 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
  • Pretest vs. Posttest Diagram
  • 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
  • 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
  • The For and Next Statements - General Form
    • For LoopIndex = 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
  • For/Next Loop Diagram
  • 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
  • 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
  • Sending Information to the Printer
    • Components appear in the Printing tab of the toolbox
    • Most professional programmers using VB use a separate utility program to format printer reports
    • Several companies sell utilities that do a nice job designing and print reports
    • VB Professional Edition and Enterprise Edition include Crystal Reports for creating reports from database files
  • 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
  • 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
  • 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
  • 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
  • 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)
  • 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
  • PrintPageEventArgs
    • PrintPageEventArgs argument has several useful properties and are used to determine the present settings
      • MarginBounds
      • PageBounds
      • PageSettings
  • Printing the Contents of 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 are a known
    • Items collections allows for printing out the actual values from the list
  • 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-has a Width property
    • Use MeasureString method of the Graphics class to determine the width of a formatted string in pixels
  • 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)
  • 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
  • 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
  • PrintPreviewDialog Component
  • The Using Block
    • System resources such as fonts can be access inside of 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
  • 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
  • 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