Question Auto search on click of alphabet

harshi

New member
Joined
Jan 7, 2013
Messages
2
Programming Experience
Beginner
Hi

I have a vb.net application which has a list of 300+ database names in one column.While performing any action on selected db name I will have to scroll the whole list to look for it. Instead, when typing one character from the keyboard, it should highlight the name starting from that character (starting character is 0-9 and a-z). I do not want a seperate search button for it. Does this require a seperate click event which loads those names starting from the typed alphabet?
 
Hi,

The simplest way to do this is to use a ComboBox to hold the Database names and then set it's AutoCompleteMode property to Suggest and it's AutoCompleteSource property to ListItems. Once you then start typing in the control the list of available Databases is then filtered for you to quickly select the correct one that you want.

Hope that helps.

Cheers,

Ian
 
Hi Ian,

Thank you for the reply. But the table loads in GridView. I do not want to make any changes in the front end. Please could you suggest ....
 
Hi,

I am assuming that you are using a DataGridView? If so, and on the basis that you do not want to modify the UI, the best that I can offer is to manipulate the Filter method of a BindingSource. To do this, if you do not have a BindingSource between your data source and the control then add one and bind the control to the BindingSource. Once done, you can then use this code to handle the DataGridView.KeyDown event to do what you want:-

VB.NET:
Private mySearchString As String
 
Private Sub CustomersDataGridView_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles CustomersDataGridView.KeyDown
    If e.KeyCode = Keys.Back Then
        If Not mySearchString = String.Empty Then
            mySearchString = mySearchString.Substring(0, mySearchString.Length - 1)
        End If
   Else
        mySearchString += e.KeyData.ToString
    End If
    CustomersBindingSource.Filter = "CompanyName Like '" & mySearchString & "%'"
End Sub

An important thing to note here is that this will only work if the DataGridView has the focus. If this does not work for you, then you could set the Form KeyPreview property to True and move this code to the Form.Keydown event but then that would depend on what else you need to do with your form?

Hope that helps.

Cheers,

Ian
 
Back
Top