Vous êtes sur la page 1sur 14

PRACTICE 10

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

8. Private Sub bttnAdd1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles bttnAdd1.Click
9. Dim ListItem As String
10. ListItem = InputBox("Enter new item's name")
11. If ListItem.Trim <> "" Then
12. sourceList.Items.Add(ListItem)
13. End If
14. 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

19. Private Sub bttnRemoveSelSrc_Click(ByVal sender As


System.Object, ByVal e As System.EventArgs) Handles
bttnRemoveSelSrc.Click
20. Dim i As Integer
21. For i = 0 To sourceList.SelectedIndices.Count - 1
22. sourceList.Items.RemoveAt(sourceList.SelectedIndices(0))
23. Next
24. 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

31. Private Sub bttnMoveAllDest_Click(ByVal sender As System.Object, ByVal


e As System.EventArgs) Handles bttnMoveAllDest.Click
32. Dim i As Integer
33. For i = 0 To sourceList.Items.Count - 1
34. destinationList.Items.Add(sourceList.Items(0))
35. sourceList.Items.RemoveAt(0)
36. Next
37. 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

45. Private Sub bttnClearLeft_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles bttnClearLeft.Click
46. sourceList.Items.Clear()
47. 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

51. Private Sub bttnMoveAllSrc_Click(ByVal sender As


System.Object, ByVal e As System.EventArgs) Handles
bttnMoveAllSrc.Click
52. Dim i As Integer
53. For i = 0 To destinationList.Items.Count - 1
54. sourceList.Items.Add(destinationList.Items(0))
55. destinationList.Items.RemoveAt(0)
56. Next
57. 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

4. Dim rnd As System.Random


5. rnd = New System.Random()
6. Dim rndChar As Char

7. Dim thisWord As String


8. Dim i, j As Integer

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

End! by Has Bunton 14

Vous aimerez peut-être aussi