ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

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.

  1. CREATE TABLE IF NOT EXISTS `employees` (
  2. `EMP_ID` int(11) NOT NULL,
  3. `F_NAME` varchar(255) DEFAULT NULL,
  4. `L_NAME` varchar(255) DEFAULT NULL,
  5. `EMAIL` varchar(255) DEFAULT NULL,
  6. `PHONENUMB` varchar(255) DEFAULT NULL
  7. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

After creating a table in the database, Insert all the records in the table that you have created.

  1. INSERT INTO `employees` (`EMP_ID`, `F_NAME`, `L_NAME`, `EMAIL`, `PHONENUMB`) VALUES
  2. (101, 'Leena', 'Kochar', 'nKochar', '123-1231-212'),
  3. (102, 'Lex', 'De Haan', 'lDe Haan', '113-2122-2121'),
  4. (103, 'Lexander', 'Hunold', 'aHunold', '211-4515-1515'),
  5. (104, 'Bruce', 'Ernst', 'bErnst', '515-541-151'),
  6. (107, 'Liana', 'Lorents', 'dLorents', '515-515-5156'),
  7. (124, 'Kevin', 'Mourgos', 'kMourgos', '559-887-333'),
  8. (141, 'Trenne', 'Rajs', 'tRajs', '212-548-989'),
  9. (142, 'Curtis', 'Davies', 'cDavies', '333-999-777'),
  10. (144, 'Peter', 'Vargas', 'pVargas', '6565-5641-87'),
  11. (207, 'hatch', 'Glets', 'hGlets', '556-5465-515'),
  12. (100, 'Steven', 'King', 'sKing', NULL);

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.

  1. Imports MySql.Data.MySqlClient
  2. Public Class Form1
  3. Public con As MySqlConnection = New MySqlConnection("server=localhost;user id=root;database=dbemployees")
  4. Public cmd As New MySqlCommand
  5. Public da As New MySqlDataAdapter
  6. Public dt As New DataTable
  7. 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.
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.

  1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2. Try
  3. dt = New DataTable
  4. 'OPENING THE CONNECTION
  5. con.Open()
  6. 'HOLDS THE DATA TO BE EXECUTED
  7. With cmd
  8. .Connection = con
  9. .CommandText = "SELECT F_NAME FROM employees"
  10. End With
  11. 'FILLING THE DATA IN THE DATATABLE
  12. da.SelectCommand = cmd
  13. da.Fill(dt)
  14. 'SET A VARIABLE AS A ROW OF DATA IN THE DATATABLE
  15. Dim r As DataRow
  16. 'CLEARING THE AUTOCOMPLETE SOURCE OF THE TEXTBOX
  17. TextBox1.AutoCompleteCustomSource.Clear()
  18. 'LOOPING THE ROW OF DATA IN THE DATATABLE
  19. For Each r In dt.Rows
  20. 'ADDING THE DATA IN THE AUTO COMPLETE SOURCE OF THE TEXTBOX
  21. TextBox1.AutoCompleteCustomSource.Add(r.Item(0).ToString)
  22. Next
  23. ''''''''''''''''''''''''
  24. Catch ex As Exception
  25. MsgBox(ex.Message)
  26. End Try
  27. 'CLOSING THE CONNECTION
  28. con.Close()
  29. da.Dispose()
  30. End Sub

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

Download here.

Facebook Comments

Check Your Domain Ranking

2 Comments

Add a Comment
  1. merci…………….

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!