Lists, Loops, - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Lists, Loops,

Description:

This chapter covers the Visual Basic looping statements. Do ... While. Do ... Until ... This property holds the entire list of items from which the user may choose ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 19
Provided by: cit62
Category:
Tags: lists | loops

less

Transcript and Presenter's Notes

Title: Lists, Loops,


1
Chapter
5
  • Lists, Loops,
  • Validation, and More

2
Introduction
  • This chapter covers the Visual Basic looping
    statements
  • Do While
  • Do Until
  • For Next
  • It also discusses the use of
  • List Boxes
  • Combo Boxes
  • As well as presenting some properties and events
    used for user input validation

3
5.1
  • Input Boxes

Input Boxes Provide a Simple Way to Gather Input
Without Placing a Text Box on a Form
4
Format of the InputBox Function
InputBox(Prompt ,Title ,Default ,Xpos
,Ypos)
  • Prompt - message to the user (required)
  • 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
  • Square brackets around Title and following
    arguments indicate these are optional

5
Sample InputBox Usage
  • strUserInput InputBox("Enter the distance.", _
    "Provide a Value", "150")
  • If the users clicks OK without entering a value,
    150 will be assigned to strUserInput due to the
    default value

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/440th inch

7
5.2
  • List Boxes

List Boxes Display a List of Items and Allow the
User to Select an Item From the List
8
ListBox Items Property
  • This property holds the entire list of items from
    which the user may choose
  • The list values may be established at run time or
    as part of the form design
  • To set list values in the form design
  • Select the list box in the Design window
  • Click on ellipses button for the Items property
  • This property is a collection, a list of values
  • Type each value on a separate line

9
ListBox Items.Count Property
  • This property holds the number of items that are
    stored in the Items property
  • Example of use
  • The number of items in the list can be assigned
    to an integer variable

If lstEmployees.Items.Count 0
Then MessageBox.Show("The list has no
items!") End If
numEmployees lstEmployees.Items.Count
10
Item Indexing
  • The Items property values can be accessed from
    your VB code
  • 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
Index Out of Range Error
  • The index of the last item is always
    list.Items.Count-1
  • Reference to an index greater than Count-1 or
    less than zero throws an exception
  • An exception handler can trap this error
  • The variable ex captures the exception thrown

Try strInput lstMonths.Items(n).ToString() Catc
h ex as Exception MessageBox.show(ex.Message) End
Try
12
ListBox SelectIndex Property
  • Use the SelectIndex property to retrieve the
    index of an item selected by the user
  • If no item is selected, the value is set to -1
    (an invalid index value)
  • Can use SelectIndex to determine if an item has
    been selected by comparing to -1
  • Example

If lstLocations.SelectedIndex ltgt -1
Then location lstLocations.Items(lstLocations.S
electedIndex) End If
13
ListBox SelectedItem Property
  • Instead of using the SelectedIndex property as
    follows
  • The SelectedItem property can be used to retrieve
    a selected item value as shown

If lstMonths.SelectedIndex ltgt -1 Then month
lstMonths.Items(lstMonths.SelectedIndex) End If
If lstMonths.SelectedIndex ltgt -1 Then month
lstMonths.SelectedItem.Value) End If
14
ListBox Sorted Property
  • Sorted is a boolean property
  • If true, causes the values in the Items property
    to be displayed in alphabetical order
  • If false, values in the Items property are
    displayed in the order they were added

15
ListBox Items.Add Method
  • Items can be added to the end of a ListBox list
    in your VB code using the Add method
  • Format is
  • ListBox.Items.Add(Item)
  • ListBox is the name of the control
  • Item is the value to add to the Items property
  • Example

lstStudents.Items.Add("Sharon")
16
ListBox Items.Insert Method
  • Items can be added at a specific position of a
    ListBox in VB code using the Insert method
  • ListBox.Items.Insert(Index, Item)
  • Index specifies position where Item is placed
  • Index is zero based similar to SelectedIndex
    property
  • Items that follow are pushed down
  • Example inserting "Jean as the 3rd item

lstStudents.Items.Insert(2, "Jean")
17
ListBox Methods to Remove Items
  • ListBox.Items.RemoveAt(Index)
  • Removes item at the specified index
  • ListBox.Items.Remove(Item)
  • Removes item with value specified by Item
  • ListBox.Items.Clear()
  • Removes all items in the Items property
  • Examples

lstStudents.Items.RemoveAt(2) remove 3rd
item lstStudents.Items.Remove(Jean) remove
item Jean lstStudents.Items.Clear() remove all
items
18
Other ListBox Methods
  • ListBox.Items.Contains(Item)
  • Returns true if Item is found in the collection
  • ListBox.Items.IndexOf(Item)
  • Returns an integer with the index position of the
    first occurrence of Item in the collection
  • Examples
  • Tutorial 5-1 provides more examples of ListBox
    controls, methods and properties

blnFound lstMonths.Items.Contains(March) intIn
dex lstMonths.Items.IndexOf(March)
Write a Comment
User Comments (0)
About PowerShow.com