1 / 26

Unit 8 List Boxes and the Do While Looping Structure Chapter 5 Lists, Loops, Validation, and More

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.

riley-bruce
Download Presentation

Unit 8 List Boxes and the Do While Looping Structure Chapter 5 Lists, Loops, Validation, and More

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Unit 8List Boxes and the Do While Looping StructureChapter 5Lists, Loops, Validation, and More

  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. Input Boxes Provide a Simple Way to Gather Input Without Placing a Text Box on a Form Input Boxes

  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. Sample InputBox Usage • userInput = InputBox("Enter the distance.", "Provide a Value", "150")

  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. List Boxes Display a List of Items and Allow the User to Select an Item From the List List Boxes

  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. 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. 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. 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. ListBox SelectedItem Property • When an item has been selected, this property, SelectedItem, contains the selected item itself

  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. 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. 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. 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. A Loop Is Part of a ProgramThat Repeats The Do While Loop

  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. Do While Flowchart • The Do While loop • If/While theexpression is true,the statement(s)are executed Expression statement(s) True False

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

  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

More Related