There is one good way to dynamically show/hide items in a ListBox - use a DataView as DataSource.
You do not need to manually Add and Remove items from your list. In fact, coding effort is very low with this approach.
Here is the extract in VB.NET, which shows the idea:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| Public Class frm_main
Dim _dataView As DataView
Private Sub MyBase_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable
dt.Columns.Add("key")
dt.Columns.Add("value")
PopulateDataTable(dt)
_dataView = New DataView(dt)
lst_items.ValueMember = "key"
lst_items.DisplayMember = "value"
lst_items.DataSource = _dataView
End Sub
[...]
Private Sub txt_search_TextChanged(sender As System.Object, e As System.EventArgs) Handles txt_search.TextChanged
_dataView.RowFilter = String.Format("Value LIKE '%{0}%'", txt_search.Text)
End Sub
End Class
|
Demo project is attached.