Hello,in this c# tutorial i will explain how to make a textbox autocomplete means user type in textbox,that data or string autocomplete.
TextBox have some important properties like AutoCompleteCustomSource, AutoCompleteMode, and AutoCompleteSource properties used to create a TextBox that automatically completes input strings by comparing the prefix being entered to the prefixes all strings from source.
Must to set the AutoCompleteSource property to CustomSource to use ,but the use of the AutoCompleteCustomSource property is optional.
1 .Create a database name as Employee using SQL SERVER.Create a Table name as EmployeeDetails in this database.
Column Name
|
Data Types
|
EmpId
|
Int [Identity Property Set =True]
|
EmpName
|
varchar(50)
|
2. Open VisualStudio.Net / Select C# / Windows Application
3. Make the design of the form as Follows.
Add a TextBox on form
Set the TextBox following properties
AutoCompleteMode:- SuggestAppend
AutoCompleteSource:- CustomSource
4.Add the following code for AutoComplete TextBox
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace AutoSearchSuggest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection cn = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=Employee;Integrated Security=True");
SqlCommand cmd;
SqlDataReader dr;
private void Form1_Load(object sender, EventArgs e)
{
cmd = new SqlCommand("SELECT EmpName FROM EmployeeDetails", cn);
cn.Open();
dr = cmd.ExecuteReader();
AutoCompleteStringCollection Collection = new AutoCompleteStringCollection();
while (dr.Read())
{
Collection.Add(dr.GetString(0));
}
txtName.AutoCompleteCustomSource = Collection;
dr.Close();
cn.Close();
}
}
}