Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

How to Select/Click 1st item in ListBox on Button_Click

I have a txtSearch box, a lstListbox (which returns search results) and a btnSearch Button.

How could I select and Click the first result in the listbox when I press the btnSearch Button and actuall have it go to the selected record.

The list box works great if I manually click on a search result but I'd like to just jump to the first result as well without having to manually click on it.

Thanks.


Code:' Your search button populates the listbox with the data from the Address column in the database/dataview
    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        'THIS PASTES TEXT THAT IS IN THE CLIPBOARD TO TXTSEARCH.TXT THEN SEARCHES.
        Dim txtdata As IDataObject = Clipboard.GetDataObject()
        If (txtdata.GetDataPresent(DataFormats.Text)) Then
            'this part sets a textboxes contents to the clipboard
            txtSearch.Text = txtdata.GetData(DataFormats.Text)
        End If
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        If Me.txtSearch.Text = "" Then Exit Sub

            dvSocSecNo.RowFilter = "SocSecNo" & " Like '%" & Trim(txtSearch.Text) & "%'"

        With ListBox1
            .Items.Clear()
            Dim i As Integer
            For i = 0 To dvSocSecNo.Count - 1
                .Items.Add(dvSocSecNo(i)("SocSecNo").ToString)
            Next
        End With

        'ListBox1.Select(0)

    End Sub
0
Matrix1000
Asked:
Matrix1000
1 Solution
 
Mikal613Commented:
lstWords.SelectedIndex = 0
0
 
davidrichardsonCommented:
     this is what i use

  If TxbSearch.Text <> "" Then
            Dim i As Int32
            For i = 0 To Dataset.Tables("TblOrders").DefaultView.Count - 1
                If Convert.ToString(Dataset.Tables("TblOrders").DefaultView.Item(i).Item("order")).EndsWith(TxbSearch.Text) Then Exit For
            Next
            BindingContext(Dataset, "TblOrders").Position = i
End If
        Call ListBox1_Click(Me, e)

this will find the item and fire the listbox click event
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now