unit 8 list boxes and the do while looping structure chapter 5 lists, loops, validation, and more

26
© 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 Chapter 5 Lists, Loops, Validation, and More

Upload: riley-bruce

Post on 31-Dec-2015

32 views

Category:

Documents


3 download

DESCRIPTION

Unit 8 List Boxes and the Do While Looping Structure Chapter 5 Lists, Loops, Validation, and More. Chapter 5 Topics. This chapter covers the Visual Basic .NET looping statements Do … While Do … Until For … Next It also discusses the use of List Boxes Combo Boxes. - PowerPoint PPT Presentation

TRANSCRIPT

© 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

Chapter 5

Lists, Loops, Validation, and More

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 2

Chapter 5 Topics

• This chapter covers the Visual Basic .NET looping statements Do … While Do … Until For … Next

• It also discusses the use of List Boxes Combo Boxes

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 3

Input Boxes

Input Boxes Provide a Simple Way to Gather Input Without Placing a Text

Box on a Form

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 4

Format of the InputBox Function

• Prompt - message to the user• Title - text for the box's title bar• Default - default text for user's input• Xpos - X coordinate for the box's position• Ypos - Y coordinate for the box's position• Title and beyond are optional arguments

InputBox(Prompt [,Title] [,Default] [,Xpos] [,Ypos])

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 5

Sample InputBox Usage

• userInput = InputBox("Enter the distance.", "Provide a Value", "150")

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 6

Xpos, Ypos, and Twips

• Xpos specifies the distance from the left of the screen to the left side of the box

• Ypos, from the top of the screen to the top of the box

• Both are specified in twips• One twip is 1/440 inch

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 7

List Boxes

List Boxes Display a List of Items and Allow the User to Select an Item

From the List

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 8

ListBox Items Property

• This property holds the list of items from which the user may choose

• The value may be established at design time and/or run time

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 9

ListBox Items.Count Property

• This property holds the number of items that are stored in the Items property

• Example of use:

If lstEmployees.Items.Count = 0 ThenMessageBox.Show("There are no items in the list!")

End If

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 10

Item Indexing

• The Item property values can be accessed under program control

• Each item value is given a sequential index The first item has an index of 0 The second item has an index of 1, etc.

• Example:

name = lstCustomers.Items(2)' Access the 3rd item value

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 11

ListBox SelectIndex Property

• The index of the user selected item is available as the value of the SelectIndex property

• If the user did not select any item, the value is set to -1 (an invalid index value)

• Example:

If lstLocations.SelectedIndex <> -1 Thenlocation = lstLocations.Items(lstLocations.SelectedIndex)

End If

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 12

ListBox SelectedItem Property

• When an item has been selected, this property, SelectedItem, contains the selected item itself

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 13

ListBox Sorted Property

• The value of this property, if true, causes the items in the Items property to be displayed in alphabetical order

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 14

ListBox Items.Add Method

• To add items to the end of a ListBox list at run time, use the Add method

• ListBox.Items.Add(Item)• Example

lstStudents.Items.Add("Sharon")

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 15

ListBox Items.Insert Method

• To add items at a specific position in a ListBox list at run time, use the Insert method

• ListBox.Items.Insert(Index, Item)• Example making the 3rd item "Jean"

lstStudents.Items.Insert(2, "Jean")

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 16

ListBox Items.Remove, Items.Clear,and Items.RemoveAt Methods

• ListBox.Items.Remove(Item) Removes the item named by value

• ListBox.Items.RemoveAt(Index) Removes the item at the specified index

• ListBox.Items.Clear() Removes all items in the Items property

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 17

The Do While Loop

A Loop Is Part of a ProgramThat Repeats

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 18

Repetition Structure (or Loop)

• Visual Basic .NET has three structures for repeating a statement or group of statements Do While Do Until For Next

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 19

Do While Flowchart

• The Do While loop• If/While the

expression is true,the statement(s)are executed Expression statement(s)

False

True

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 20

Do While Syntax

• "Do", "While", and "Loop" are new keywords• The statement, or statements are known as

the body of the loop

Do While expressionstatement(s)

Loop

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 21

Do While Example

Private Sub btnRunDemo_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles btnRunDemo.Click' Demonstrate the Do While loop

Dim count As Integer = 0Do While count < 10

lstOutput.Items.Add("Hello")count += 1

LoopEnd Sub

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 22

Infinite Loops

• Generally, if the expression is true and, hence the starts executing:

• Something with the body of the loop must eventually make the test expression false

• Otherwise, the Do While loop will continuously loop forever - called an infinite loop

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 23

Counters

• Variables called counters are frequently used to control Do While loops (see count in the previous example

• Counters are invariably initialized before the loop begins (above: Dim count As Integer = 0)

• They are also usually modified within the body of the loop (above: count += 1)

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 24

Pretest vs. Posttest Loops

• The preceding Do While loops were written in their pretest syntax

• The expression is always tested before the body of the loop is executed

• Do While loops also have a posttest form• In these, the body of the loop is always

executed first, then the expression is evaluated to check to see if additional iterations are needed

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 25

Posttest Do While Syntax and Flowchart

• The statement(s) willalways be done once,irrespective of theexpression used

Dostatement(s)

Loop While expression

Expression

statement(s)

False

True

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 26

Example: Keeping a Running Total

count = 1 ' Initialize the countertotal = 0 ' Initialize totalDo

input = InputBox("Enter the sales for day " & _count.ToString, "Sales Amount Needed")

If input <> "" Thensales = CDec(input) total += sales ' Add sales to totalcount += 1 ' Increment the counter

End IfLoop While count <= 5