Académique Documents
Professionnel Documents
Culture Documents
The ListBox
ListBox,, ComboBox Control
Visual Basic.NET
by Has Bunton
1
Basic Properties
Items: a collection that holds and
manipulates the items on the control.
MultiColumn: to work with ColumnWidth
property
SelectionMode
Sorted
Text: return the selected text on the
control
by Has Bunton 2
The Items Collection (1)
You can manipulate a ListBox’s items
using the following items collection
members:
Add
Clear
Count
Insert: syntax is LB.Items.Insert(index, item)
Remove
by Has Bunton 3
The Items Collection (2)
Contains: return true/false indicating
whether the control contains the
object or not.
Example:
by Has Bunton 4
The List Demo Project (1)
Objective: to demonstrate the basic
operations of the ListBox control
Design: see the following interface
Use default property
for the left ListBox
For the right ListBox,
set Sorted property to
true and MultiSelect
property to
MultiExtended
by Has Bunton 5
The List Demo Project (2) - Coding
1. Private Sub btttnAdd2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btttnAdd2.Click
2. Dim ListItem As String
3. ListItem = InputBox("Enter new item's name")
4. If ListItem.Trim <> "" Then
5. destinationList.Items.Add(ListItem)
6. End If
7. End Sub
by Has Bunton 6
The List Demo Project (3) - Coding
15. Private Sub bttnRemoveSelDest_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
bttnRemoveSelDest.Click
16. If destinationList.SelectedItems.Count > 0 Then
destinationList.Items.Remove(destinationList.SelectedItem)
17. End If
18. End Sub
by Has Bunton 7
The List Demo Project (4) - Coding
25. Private Sub bttnMoveSrc_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles bttnMoveSrc.Click
26. If destinationList.SelectedItems.Count > 0 Then
27. sourceList.Items.Add(destinationList.SelectedItem)
28. destinationList.Items.RemoveAt(destinationList.SelectedIndex)
29. End If
30. End Sub
by Has Bunton 8
The List Demo Project (5) - Coding
38. Private Sub bttnMoveDest_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles bttnMoveDest.Click
39. Dim i As Integer
40. For i = 0 To sourceList.SelectedIndices.Count - 1
41. destinationList.Items.Add(sourceList.Items(sourceList.SelectedIndices(0)))
42. sourceList.Items.Remove(sourceList.Items(sourceList.SelectedIndices(0)))
43. Next
44. End Sub
by Has Bunton 9
The List Demo Project (6) - Coding
48. Private Sub bttnClear2_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
bttnClear2.Click
49. destinationList.Items.Clear()
50. End Sub
by Has Bunton 10
Searching Item in ListBox Control
You can now use FindString and
FindStringExact to locate any item in
the list.
Note: Both methods perform case-
insensitive searches
by Has Bunton 11
The ListBoxFind Application (1)
Objectives: This application
demonstrates the use of FindString
and FindStringExact methods
Design:
by Has Bunton 12
The ListBoxFind Application (2)
Coding:
1. Private Sub bttnPopulate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles bttnPopulate.Click
2. Dim wordLen As Integer
3. Dim Nwords As Integer = 9999
9. For i = 0 To Nwords
10. wordLen = CInt(rnd.NextDouble * 20 + 1)
11. thisWord = ""
12. For j = 0 To wordLen
13. rndChar = Chr(65 + CInt(rnd.Next(0, 25)))
14. thisWord = thisWord & rndChar
15. Next
16. ListBox1.Items.Add(thisWord)
17. Next
18. End Sub
by Has Bunton 13
The ListBoxFind Application (3)
1. Private Sub bttnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
bttnFind.Click
2. Dim srchWord As String
3. Dim wordIndex As Integer
4. srchWord = InputBox("Enter word to search for")
5. wordIndex = ListBox1.FindStringExact(srchWord)
6. If wordIndex >= 0 Then
7. MsgBox("Index = " & wordIndex.ToString & " =" & _
8. (ListBox1.Items(wordIndex)).ToString, , "EXACT MATCH")
9. ListBox1.TopIndex = wordIndex
10. ListBox1.SelectedIndex = wordIndex
11. Else
12. wordIndex = ListBox1.FindString(srchWord)
13. If wordIndex >= 0 Then
14. MsgBox("Index = " & wordIndex.ToString & " =" & _
15. (ListBox1.Items(wordIndex)).ToString, , "NEAR MATCH")
16. ListBox1.TopIndex = wordIndex
17. ListBox1.SelectedIndex = wordIndex
18. Else
19. MsgBox("Item " & srchWord & " is not in the list")
20. End If
21. End If
22. End Sub