hello, am fairly new to VB. Net and am trying to develop an application. I want to implement auto complete function in one of my text boxes. I want my text box to show a list of items that are stored in my database when a user enters a particular letter.
thanks.

Just type autocomplete into the search box in the top right corner and you will find plenty of references.

Here is an example

Dim srce As New AutoCompleteStringCollection()
srce.AddRange({"January", "February", "March", "April", "May", "June",
              "July", "August", "September", "October", "November", "December"})

txtSample.AutoCompleteMode = AutoCompleteMode.Suggest
txtSample.AutoCompleteSource = AutoCompleteSource.CustomSource
txtSample.AutoCompleteCustomSource = srce

hi
thanks for your response but
in AddRange, should I include the dataset from which I want to display the values from.

thanks.

Iterate through the dataset and add the values to the source collection as in

Dim con As New SqlConnection("Server=.\SQLEXPRESS;Database=PUBS;Trusted_Connection=yes;")
Dim cmd As New SqlCommand("SELECT au_lname FROM authors", con)

con.Open()

Dim rdr As SqlDataReader = cmd.ExecuteReader

Dim srce As New AutoCompleteStringCollection()

Do While rdr.Read()
    srce.Add(rdr("au_lname"))
Loop

rdr.Close()
con.Close()

txtSample.AutoCompleteCustomSource = srce

Or using Linq you could do

Dim con As New SqlConnection("Server=.\SQLEXPRESS;Database=PUBS;Trusted_Connection=yes;")
Dim dat As New DataTable
Dim daa As New SqlDataAdapter("SELECT au_lname FROM authors", con)

con.Open()
daa.Fill(dat)
con.Close()

txtSample.AutoCompleteCustomSource.AddRange((From row In dat.Rows.Cast(Of DataRow)() Select CStr(row("au_lname"))).ToArray())

hi

thanks but my database is in access. but an trying to use your procedures if I succeed I will let u know.

hi

I figured it out but one problem though, my code is not picking data from exactly the database, its picking from a custom source.

dim r as Emma.emmadataset.items row=etc.rows

dim itemc  = from n in r.item name select n

txtsample.auto complete mode= autocompletemode.suggested

 txtsample.autocompletecustomsource.autocompletesource.custom source

txtsample.autocompletecustomsource.add(itemc.to array())

please advise.

Edited 1 Year Ago by Reverend Jim: fixed code formatting

I can't advise because I don't know what your custom source is. If you are using Access instead of MS SQL just use OleDb instead of SqlClient.

This article has been dead for over six months. Start a new discussion instead.