Tech Blog‎ > ‎

VB.NET - Hidden items in a ListBox

posted Jun 21, 2012, 11:19 AM by Victor Zakharov   [ updated Jun 21, 2012, 11:19 AM ]
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:

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.
ċ
HideItemsInListBox.zip
(12k)
Victor Zakharov,
Jun 21, 2012, 11:19 AM
Comments