I have this block of code where l run through a table and display matched results in a listbox.
It works fine, but my question is how do l enable the user to select an item in the listbox once it is displayed?
Suggestions are welcomed as always. Thanks

       Try
        adapter.Fill(dt)
        For Each dr In dt.Rows
            Dim cns
            Dim bytes As Byte() = Nothing
            bytes = dr.Item("FingerP")
            cns = dr.Item("CaseFileNumber")

            Dim template = New DPFP.Template()
            template.DeSerialize(bytes)
            'Perform match
            matcher.Verify(FeatureSet, template, matchResult)
            If matchResult.Verified Then
                EventHandlerStatus = Gui.EventHandlerStatus.Success
                MessageBox.Show("Verified!")
                'Exit For 'success
                'txtcasenumber.Text = cns
                'ListView1.Items.Add(cns)
                listbox.Items.Add(cns)
                'DataGridView1 = cns

            End If
            If Not matchResult.Verified Then EventHandlerStatus = Gui.EventHandlerStatus.Failure

Dani AI

Generated

Since is already adding case numbers to a WinForms ListBox, the control is selectable by default. Common pitfalls are SelectionMode, Enabled/Visible being false, or the UI being blocked while you fill the list (a long loop on the UI thread prevents the user from clicking). pointed you toward reading the selection — below are compact, practical examples and troubleshooting notes you can apply immediately.

' Single-selection: show chosen item in a TextBox
Private Sub listBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles listBox1.SelectedIndexChanged
    Dim lb As ListBox = DirectCast(sender, ListBox)
    If lb.SelectedIndex = -1 Then Return
    Dim chosen As String = lb.SelectedItem.ToString()
    txtCaseNumber.Text = chosen
End Sub
' Multi-selection: gather all selected items
Private Sub listBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles listBox1.SelectedIndexChanged
    Dim selected As New List(Of String)
    For Each it As Object In listBox1.SelectedItems
        selected.Add(it.ToString())
    Next
    txtSelected.Text = String.Join(", ", selected)
End Sub

Tips and quick fixes:

  • Ensure listBox1.Enabled = True and listBox1.SelectionMode is SelectionMode.One (or MultiSimple/MultiExtended if you want multiple picks).
  • If you populate items in a loop that runs on the UI thread, the form won’t respond until the loop finishes. Populate from a Task/BackgroundWorker and marshal UI updates with Invoke/BeginInvoke, or add items on the UI thread but return control so the user can click.
  • To pre-select programmatically: If listBox1.Items.Count > 0 Then listBox1.SelectedIndex = 0.
  • If this were ASP.NET WebForms instead of WinForms, use AutoPostBack = True and handle SelectedIndexChanged server-side (or use client-side JS for immediate UX).

These points should let the user click an item and let your code read which case number they chose; tie the selection handler into whatever action you want (populate txtcasenumber, enable a button, open the record, etc.).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.