How to Create an AutoComplete in a TextBox in VB.Net

2
3112

This tutorial is all about How to Create an AutoComplete in a TextBox in VB.Net.
When searching a record from the database using a TextBox in VB.Net, commonly, it’s hard for us to search the exact record. So, In this tutorial, I created an autosuggest textbox, so that, it will be easy for you to search the correct record in the table. For instance, the word “itsourcecode”.

 

Let’s begin:
First, Create a database and name it “employeesdb” in the MySQL Database. after that, set this query for creating a table.
[mysql] CREATE TABLE IF NOT EXISTS employees (
EMP_ID int(11) NOT NULL,
F_NAME varchar(255) DEFAULT NULL,
L_NAME varchar(255) DEFAULT NULL,
EMAIL varchar(255) DEFAULT NULL,
PHONENUMB varchar(255) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
[/mysql] After creating a table in the database, Insert all the records in the table that you have created.
[mysql] INSERT INTO employees (EMP_ID, F_NAME, L_NAME, EMAIL, PHONENUMB) VALUES
(101, ‘Leena’, ‘Kochar’, ‘nKochar’, ‘123-1231-212’),
(102, ‘Lex’, ‘De Haan’, ‘lDe Haan’, ‘113-2122-2121’),
(103, ‘Lexander’, ‘Hunold’, ‘aHunold’, ‘211-4515-1515’),
(104, ‘Bruce’, ‘Ernst’, ‘bErnst’, ‘515-541-151’),
(107, ‘Liana’, ‘Lorents’, ‘dLorents’, ‘515-515-5156’),
(124, ‘Kevin’, ‘Mourgos’, ‘kMourgos’, ‘559-887-333’),
(141, ‘Trenne’, ‘Rajs’, ‘tRajs’, ‘212-548-989’),
(142, ‘Curtis’, ‘Davies’, ‘cDavies’, ‘333-999-777’),
(144, ‘Peter’, ‘Vargas’, ‘pVargas’, ‘6565-5641-87’),
(207, ‘hatch’, ‘Glets’, ‘hGlets’, ‘556-5465-515’),
(100, ‘Steven’, ‘King’, ‘sKing’, NULL);
[/mysql] Open the Microsoft Visual Studio 2008 and create a new Windows Form Application. After that, Put a TextBox and a Label.
autocompleteform
Click the TextBox and go to the properties. In the properties, select the Suggest for the AutoComplete mode and select Custom Source for the AutoComplete Source.
autocompleteproperties
After setting up the properties of a textbox, go to the solution explorer and hit the code view.
autocompletecodeview
In the code view, set up your connection and declare all the classes that are needed.
[vbnet] Imports MySql.Data.MySqlClient
Public Class Form1
Public con As MySqlConnection = New MySqlConnection(“server=localhost;user id=root;database=dbemployees”)
Public cmd As New MySqlCommand
Public da As New MySqlDataAdapter
Public dt As New DataTable
End Class
[/vbnet] Reminder: Before setting up your connection make sure that you will add MySql. Data. dll as your reference. So you will not encounter errors.
6. After the declaration, go back to the Form Design and double click it. Now, you have to set up the AutoComplete in the TextBox on the first load of the Form.
[vbnet] Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
dt = New DataTable
‘OPENING THE CONNECTION
con.Open()
‘HOLDS THE DATA TO BE EXECUTED
With cmd
.Connection = con
.CommandText = “SELECT F_NAME FROM employees”
End With
‘FILLING THE DATA IN THE DATATABLE
da.SelectCommand = cmd
da.Fill(dt)
‘SET A VARIABLE AS A ROW OF DATA IN THE DATATABLE
Dim r As DataRow
‘CLEARING THE AUTOCOMPLETE SOURCE OF THE TEXTBOX
TextBox1.AutoCompleteCustomSource.Clear()
‘LOOPING THE ROW OF DATA IN THE DATATABLE
For Each r In dt.Rows
‘ADDING THE DATA IN THE AUTO COMPLETE SOURCE OF THE TEXTBOX
TextBox1.AutoCompleteCustomSource.Add(r.Item(0).ToString)
Next
””””””””””””
Catch ex As Exception
MsgBox(ex.Message)
End Try
‘CLOSING THE CONNECTION
con.Close()
da.Dispose()
End Sub
[/vbnet] Complete Source Code is included. Download it and run on your computer.

Download here.

 

Readers might read also:

 

 

Facebook Comments

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.