Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 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.

Similar presentations


Presentation on theme: "© 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."— Presentation transcript:

1 © 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

2 © 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

3 © 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

4 © 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])

5 © 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")

6 © 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

7 © 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

8 © 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

9 © 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 Then MessageBox.Show("There are no items in the list!") End If

10 © 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

11 © 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 Then location = lstLocations.Items(lstLocations.SelectedIndex) End If

12 © 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

13 © 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

14 © 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")

15 © 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")

16 © 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

17 © 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 Program That Repeats

18 © 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

19 © 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

20 © 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 expression statement(s) Loop

21 © 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 = 0 Do While count < 10 lstOutput.Items.Add("Hello") count += 1 Loop End Sub

22 © 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

23 © 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 )

24 © 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

25 © 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 25 Posttest Do While Syntax and Flowchart The statement(s) will always be done once, irrespective of the expression used Do statement(s) Loop While expression Expression statement(s) False True

26 © 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 26 Example: Keeping a Running Total count = 1' Initialize the counter total = 0' Initialize total Do input = InputBox("Enter the sales for day " & _ count.ToString, "Sales Amount Needed") If input <> "" Then sales = CDec(input) total += sales ' Add sales to total count += 1 ' Increment the counter End If Loop While count <= 5


Download ppt "© 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."

Similar presentations


Ads by Google