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

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

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.

What is Visual Basic’s purpose?

The third-generation programming language was created to aid developers in the creation of Windows applications.

It has a programming environment that allows programmers to write code in.exe or executable files.

They can also utilize it to create in-house front-end solutions for interacting with huge databases.

It is because the language allows for continuing changes, you can keep coding and revising your work as needed.

However, there are some limits to the Microsoft Visual Basic download.

If you want to make applications that take a long time to process, this software isn’t for you.

That implies you won’t be able to use VB to create games or large apps because the system’s graphic interface requires a lot of memory and space.

Furthermore, the language is limited to Microsoft and does not support other operating systems.

What are the most important characteristics of Visual Basic?

Microsoft Visual Basic for Applications Download, unlike other programming languages, allows for speedier app creation.

It has string processing capabilities and is compatible with C++, MFC, and F#. Multi-targeting and the Windows Presentation Framework are also supported by the system, allowing developers to create a variety of Windows apps, desktop tools, metro-style programs, and hardware drivers.

For instance, the word “itsourcecode”.

This tutorial can answer the questions on How to Create an Autocomplete in a textbox in vb.net.

Let’s begin:

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

Step 1: First, create a database and name it “employeesdb” in the MySQL Database. after that, set this query for creating a table.

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;

Step 2: Then creating a table in the database, Insert all the records in the table that you have created.

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);

autocompleteform
autocompletecodeview

Step 3: Open the Microsoft Visual Studio 2008 and create a new Windows Form Application. After that, Put a TextBox and a Label.

Step 4: 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
Step 5: After setting up the properties of a textbox, go to the solution explorer and hit the code view.

Step 6: In the code view, set up your connection and declare all the classes that are needed.

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

Reminder: Before setting up your connection make sure that you will add MySql. Data. dll as your reference. So you will not encounter errors. Follow the instructions here the complete guide to add mysql.data.dll.

Step 7: 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.

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

Complete Source Code is included. Download it and run on your computer.

Download here.

Inquiries

If you have any questions or suggestions about this tutorial on how to create an autocomplete in textbox using vb.net. please leave a comment below.

Readers might read also:

1 thought on “How to Create an AutoComplete in a TextBox in VB.net”

Leave a Comment