260 likes | 372 Views
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.
E N D
Unit 8List Boxes and the Do While Looping StructureChapter 5Lists, 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
Input Boxes Provide a Simple Way to Gather Input Without Placing a Text Box on a Form Input Boxes
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])
Sample InputBox Usage • userInput = InputBox("Enter the distance.", "Provide a Value", "150")
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
List Boxes Display a List of Items and Allow the User to Select an Item From the List List Boxes
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
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
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
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
ListBox SelectedItem Property • When an item has been selected, this property, SelectedItem, contains the selected item itself
ListBox Sorted Property • The value of this property, if true, causes the items in the Items property to be displayed in alphabetical order
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")
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")
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
A Loop Is Part of a ProgramThat Repeats The Do While Loop
Repetition Structure (or Loop) • Visual Basic .NET has three structures for repeating a statement or group of statements • Do While • Do Until • For Next
Do While Flowchart • The Do While loop • If/While theexpression is true,the statement(s)are executed Expression statement(s) True False
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
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
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
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)
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
Posttest Do While Syntax and Flowchart • The statement(s) willalways be done once,irrespective of theexpression used Do statement(s) Loop While expression statement(s) Expression True False
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