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

Posted on 2004-11-17
Last Modified: 2010-04-23
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.


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
        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
            Dim i As Integer
            For i = 0 To dvSocSecNo.Count - 1
        End With


    End Sub
Question by:Matrix1000
    LVL 48

    Accepted Solution

    lstWords.SelectedIndex = 0
    LVL 7

    Expert Comment

         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
                BindingContext(Dataset, "TblOrders").Position = i
    End If
            Call ListBox1_Click(Me, e)

    this will find the item and fire the listbox click event

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now