Creating A "Search TextBox"

Tyecom

Well-known member
Joined
Aug 8, 2007
Messages
78
Programming Experience
Beginner
I'm trying to create a search textbox that will bring up titles as I type them. For instance, when I start typing D..all titles that begin with "D" will appear in drop down menu. I found a code that I believe does this but I'm getting error messages. The error message I am getting is: "cmbMovies not declared" and "CONN_STRING not declared". I created a DataSource named cmbMovies and it did not solve the problem. Am I looking at this correctly? Here is the code:

Imports System
Imports System.Configuration
Imports System.Data.SqlClient

Public Class Form1

Private Sub txtMovieName_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtMovieName.TextChanged

Dim DtMovies As DataTable
Dim Query As String = "SELECT idMovie, movieName From tblMovies "

Try
If (txtMovieName.Text.Trim.Length = 0) Then
MoviesSample.DataSource = Nothing
Return
End If
MovieSample.DataSource = Nothing

For len As Int32 = 0 To txtMovieName.Text.Split(Space(1)).Length
If (len = 0) Then
Query &= "Where movieName Like '%" & txtMovieName.Text.Split(Space(1))(len) & "%'"
Else
If (len = txtMovieName.Text.Split(Space(1)).Length) Then
Exit For
End If
Query &= vbCrLf & " AND " & vbCrLf & "movieName Like '%" & txtMovieName.Text.Split(Space(1))(len) & "%'"
End If
Next

Dim Conn As SqlConnection = New SqlConnection(CONN_STRING)
Conn.Open()
Dim sqlCmd As SqlCommand = New SqlCommand
With sqlCmd
.Connection = Conn
.CommandText = Query
.CommandType = CommandType.Text
End With
Dim sqlDa As SqlDataAdapter = New SqlDataAdapter(sqlCmd)
DtMovies = New DataTable
sqlDa.Fill(DtMovies)
MovieSample.DataSource = Nothing
If (DtMovies IsNot Nothing) Then
With MovieSample
.ValueMember = "idMovie"
.DisplayMember = "movieName"
.DataSource = DtMovies
End With
End If

Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class
 
I'm not sure about using a textbox for this, but normally I use a combobox with auto complete.
 
What does the auto complete do? And you're right, I am using a ComboBox. Sorry for leaving that out.
 
If you turn on autocomplete, and select the source as your data source its behavior will be as you discribed
 
They have three different types of "Auto Completes":

AutoCompleteCustomSource
AutoCompleteMode
AutoCompleteSource

Which one do you recommend?
 
Those are not "different types" of autocomplete, those are different options...

In the app that I'm currently working on it is as follows

AutoCompleteCustomSource : (Collection)
AutoCompleteMode : SuggestAppend
AutoCompleteSource : ListItems

Depending on how you filled your data would depend on your options...
 
Thanks ThirteenTwenty, I really appreciate your assistance. I'll try it and let you know. I don't know if I'm trying to over complicate this or what.
 
I wish I could find cool links like that
 
Back
Top