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.

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.
Chapter 7 Lists, Loops, and Printing Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
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.
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.
C# Programming: From Problem Analysis to Program Design1 Programming Based on Events C# Programming: From Problem Analysis to Program Design 3 rd Edition.
Chapter 7 Lists, Loops, and Printing
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.
Chapter 2 –Visual Basic, Controls, and Events
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.
CHAPTER SIX Loop Structures.
CHAPTER 6 Loop Structures.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Introduction to Graphical User Interfaces. Objectives * Students should understand what a procedural program is. * Students should understand what an.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
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.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
 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.
XP New Perspectives on Microsoft Word 2002 Tutorial 31 Microsoft Word 2002 Tutorial 3 – Creating a Multiple-Page Report.
Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops.
The Repetition Process in Visual Basic. The Repetition Process The capability to repeat one or more statements as many times as necessary is what really.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Programs 9.4 Graphics.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved CheckWriter Application Introducing Graphics and Printing.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
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
Chapter 7 Lists, Loops, and Printing Programming In Visual Basic.NET.
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
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.
Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.
Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed.
Controlling Program Flow with Looping Structures
Unit 6 Repetition Processing Instructor: Brent Presley.
1 Printing a Document. 2 Objectives You will be able to print a real multipage document.
COMPUTER PROGRAMMING I 3.01 Apply Controls Associated With Visual Studio Form.
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.
COMPUTER PROGRAMMING I 3.01 Apply Controls Associated With Visual Studio Form.
COMPUTER PROGRAMMING I Apply Procedures to Develop List Box and Combo Box Objects.
Apply Procedures to Develop Menus, List Box and Combo Box Objects
Using Procedures and Exception Handling
Apply Procedures to Develop Menus, List Box and Combo Box Objects
Repeating Program Instructions
Microsoft Visual Basic 2005: Reloaded Second Edition
Tutorial 3 – Creating a Multiple-Page Report
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
Overview of the IDE Visual Studio .NET is Microsoft’s Integrated Development Environment (IDE) for creating, running and debugging programs (also.
Presentation transcript:

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 Need to add to a list

Filling a List at Design Time List of items in a ListBox or ComboBox is a collection. VB collections are objects that have properties and methods The Items property lets you add strings to the collection.

Referencing the Items Collection Use the index of the item to reference a specific item in the collection. The index is zero based, so the first item in the list is index position zero. The index number of currently selected item is stored in the SelectedIndex property. SelectedIndex = -1 if no item in the list is currently selected. The property Count is the number of items in the list. SchoolsListBox.Items(1) = “Stanford"

Adding Items at Run Time The method add() is used to add new items to the list at run time. General Form: Examples: Object.Items.Add(ItemValue) SchoolsListBox.Items.Add("Harvard") MajorsComboBox.Items.Add(MajorString) Use the method insert() to add new items to the list at run time in a specific location (index position) in the collection. General Form: Examples: Object.Items.Insert(IndexPosition, ItemValue) SchoolsListBox.Items.Insert(0, "Harvard") MajorsComboBox.Items.Insert(1, MajorsString)

Removing Items at Run Time The Items method Remove() removes an item by specifying the text of the item to be deleted. General Form: Example: The Items method RemoveAt() removes an item by index from the list. General Form: Example: SchoolsComboBox.Items.Remove(“Brookdale”) Object.Items.Remove(TextString) Object.Items.RemoveAt(IndexPosition) NamesListBox.Items.RemoveAt(0)

Useful List Box and Combo Box Events TextChanged Event (ComboBox only) – User types text into combo box Enter Event – User tabs to the control and control gets focus. Leave Event – User tabs out of the control and control loses focus. To add code to these events, double-click the event name in the Properties window after clicking the Events button. The editor will create the procedure header for you.

Looping Statements A loop repeats a series of instructions. An iteration is a single execution of the statement(s) in the loop. Looping statements terminate based on a specified condition: Execution of the loop continues while a condition is True or until a condition is True. The loop executes an unpredictable and variable number of times. Or execution of the loop continues a predictable and fixed number of times.

The Do/Loop Statement in Visual Basic Do While condition 'Looping statements Loop Do Until condition 'Looping statements Loop Pre-test Loops: test at top of loop Post-test Loops: test at bottom of loop Do 'Looping statements Loop While condition Do 'Looping statements Loop Until condition

Do/Loop Examples Dim x As Integer = 1 Do While x < 5 Debug.Writeline(x.ToString()) x += 1 Loop Dim x As Integer = 1 Do Debug.Writeline(x.ToString()) x += 1 Loop Until x >= 5

For/Next Loops in Visual Basic Used to repeat statements in a loop a specific number of times. Uses a numeric counter variable, called a loop index, which is tested to determine the number of times the statements inside the loop will execute. The loop index is automatically incremented at the bottom of the loop on each iteration. Step can be included to specify the incrementing amount to increment the loop index. If Step is not included, it is assumed to be 1; Step can be a negative number. For Index [As Datatype] = Initial To End [Step Increment] 'Looping statements Next [LoopIndex]

Do/Loop Examples For index As Integer = 6 To 10 Debug.Writeline(index.ToString()) Next index Dim index As Integer For index = 11 To 15 Debug.Writeline(index.ToString()) Next For index As Integer = 5 To 1 Step -1 Debug.Writeline(index.ToString()) Next index

Exiting a Loop Abnormally In some situations, you may need to completely exit a loop before it has completed all iterations. Other times you may need to stay in the loop, but skip the current iteration and go onto the next one. To completely exit a loop early, use – Exit Do for the Do/Loop statements – Exit For for the For/Next statement To skip the current iteration of the loop and go onto the next iteration, use – Continue Do for the Do/Loop statements – Continue For for the For/Next statement

2 More Examples For index As Integer = 6 To Max If index > 100 Exit For End If Debug.Writeline(index.ToString()) Next index Dim x As Integer = 1 Do While x < 5 If x = 3 Then x += 1 Continue Do Endif Debug.Writeline(x.ToString()) x += 1 Loop

Sending Information to the Printer Two steps required for adding print features to an application: – The Easy Step: Add the components and call the methods that perform a Print or a Print Preview operation. – The Hard Step: Build the page(s) of formatted output that will be printed or previewed. Many programmers use separate print utilities independent of Visual Studio for formatting the output to be printed. VB Professional Edition and Enterprise Edition include Crystal Reports for creating reports from database files.

The Easy Step: Adding Print Components Components to add for printing appear in the Printing tab of the toolbox. Make sure you have buttons or menu items on your form for printing or preview printing. Drag either (or both) the PrintDocument component or the PrintPreviewDialog component to your Design Window. (They will appear below in the Component Tray

The Easy Step: Adding Event Code In the event handler for your Print button or menu item, call the Print() method of the PrintDocument component: PrintDocument1.Print() In the event handler for your Print Preview button or menu item, add the following two lines of code: PrintPreviewDialog1.Document = PrintDocument1 PrintPreviewDialog1.ShowDialog()

Setting Up the Print Output Double-click on the PrintDocument component to expose the PrintPage event handler. A PrintPage event is fired once for each page to be printed, and is referred to as a callback. BeginPrint and EndPrint are two other events also fired at the beginning and end of the printing. The 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 Hard Step: Formatting the Print Page 1. Set up the font, line height, and (x,y) position of where to print the first line of the page. 2. For every line on the page: 2.1 Set up the text to be printed 2.2 Print the text 2.3 Increment the y coordinate for the next line Notes: Use properties of e (of type PrintPageEventArgs ) to obtain useful information about setting up the (x,y) position. Use the Graphics object’s DrawString() method for displaying the text.

The Page Coordinate System The units of measurement on the page are points (pixels). The X coordinate is the horizontal distance going across the page left to right. The Y coordinate is the vertical distance from the top of the page going down. Position (0,0) is the upper left corner of the drawing area

Formatting Step 1: Set Up The PrintPageEventArgs argument, e, has several useful properties that are used to determine the (x,y) settings. Dim xLoc As Single Dim yLoc As Single xLoc = e.MarginBounds.Left yLoc = e.MarginBounds.Top Create a Font object to specify how the output looks: Dim PrintFont As New Font(‘Arial’, 12) Dim LineHeight As Single = PrintFont.GetHeight + 2

Formatting Step 2: Printing the Line Use the Graphics method DrawString() to display a string (one line) of the information to be printed. DrawString() has the following general form: The Graphics object comes from the PrintPageEventsArg, e. Line is String, MyFon t is a Font object, MyBrush is a Brush object, and Xcoord and Ycoord are both Single. Example DrawString(Line, MyFont, MyBrush, Xcoord, Ycoord) e.Graphics.DrawString(“Print this test string”, PrintFont, Brushes.Black, xLoc, yLoc)

Putting It All Together into Callback Private Sub PrintDocument1_PrintPage( ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage Dim xLoc, yLoc As Single xLoc = e.MarginBounds.Left yLoc = e.MarginBounds.Top Dim PrintFont As New Font(‘Arial’, 12) Dim LineHeight As Single = PrintFont.GetHeight + 2 e.Graphics.DrawString(“Print test string”, PrintFont, Brushes.Black, xLoc, yLoc) yLoc += LineHeight End Sub

A Final Example (No Printing!) Write a Visual Basic application that lists all the ways you can order a hamburger with 3 toppings: Ketchup, Pickles & Onions. Each topping can be ordered 2 ways: with it or without it. So there are 2x2x2 = 2 3 = 8 different ways to order a hamburger with just 3 toppings. This example will use nested loops. A nested loop is one loop inserted inside of another- a common programming practice. We won’t get fancy and include a form UI. We’ll just use the Intermediate Window for output.

For ketchup As Integer = 0 To 1 For pickles As Integer = 0 To 1 For onions As Integer = 0 To 1 If ketchup = 0 Then Debug.WriteLine(“Ketchup: No ”) Else Debug.WriteLine(“Ketchup: Yes ”) End If If pickles = 0 Then Debug.WriteLine(“Pickles: No ”) Else Debug.WriteLine(“Pickles: Yes ”) End If If onions = 0 Then Debug.WriteLine(“Onions: No ”) Else Debug.WriteLine(“Onions: Yes ”) End If Debug.WriteLine(ControlChar.NewLine) Next onions Next pickles Next ketchup