Search Textbox with dropdown in vb.net

you can create a drop-down textbox somthing like Auto suggest Google search. when will you type a letter to this textbox then it will show the words starting with letter in drop down.

Let’s suppose if you type ‘a’ into textbox then textbox will suggest all words starting with letter ‘a’ in drop down.

drop-down textbox

You can do this with the help of AutoCompleteMode property in Textbox control. This property automatically matches the input string. This property is very useful for frequently searching strings. If there is duplication occurs in source data then the AutoCompleteMode property omits the duplication and display only once. We need to use AutoCompleteMode and AutoCompleteSource property must be used together.

Example

In the following example textbox control named TextBox1 suggest us employee names from the database according to input string(like picture)

  1. Private Sub FillSearchResult()
  2.         Dim AutoComp As New AutoCompleteStringCollection()
  3.         Dim dsSerch As New DataSet
  4.  
  5.         Dim ConStr As String = "user id=sa;password=aeiouy;Initial Catalog=EmployeeDatabase;Data Source= PSPL-PC6"
  6.         Dim sqlCon As New SqlClient.SqlConnection(ConStr)
  7.         sqlCon.Open()
  8.         Dim Str As String = "select empname from tbl_Employee"
  9.         Dim SqlCom As New SqlClient.SqlCommand(Str, sqlCon)
  10.         Dim sqlAdap As New SqlClient.SqlDataAdapter(SqlCom)
  11.         sqlAdap.Fill(dsSerch)
  12.         For i As Integer = 0 To dsSerch.Tables(0).Rows.Count - 1
  13.             AutoComp.Add(dsSerch.Tables(0).Rows(i)(0).ToString())
  14.         Next
  15.  
  16.         TextBox1.AutoCompleteMode = AutoCompleteMode.Suggest
  17.         TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
  18.         TextBox1.AutoCompleteCustomSource = AutoComp
  19. End Sub