Autocomplete textbox in c# windows application
In this article I, we learn about the auto-completed
textbox in windows application, many
project we need this thing , like if we want to select a student name then we
can get very easily this.
Database:-
Design:-
Code behind(C#):-
using System;
using
System.Collections.Generic;
using
System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using
System.Windows.Forms;
using
System.Data.SqlClient;
namespace
WindowsFormsApplication2
{
public partial class Form1 : Form
{
AutoCompleteStringCollection
namecollection = new AutoCompleteStringCollection();
public
Form1()
{
InitializeComponent();
}
private
void Form1_Load(object
sender, EventArgs e)
{
SqlConnection
con = new SqlConnection("Data Source=NEERAJ-PC;Initial
Catalog=CodeSolution;Persist Security Info=True;User ID=sa; password=12345678");
con.Open();
SqlCommand
cmd = new SqlCommand("Select distinct [Name] from [record] order by
[Name] asc",con);
SqlDataReader
dr = cmd.ExecuteReader();
if
(dr.HasRows == true)
{
while
(dr.Read())
namecollection.Add(dr["Name"].ToString());
}
con.Close();
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox1.AutoCompleteCustomSource =
namecollection;
}
}
}
Out Put:-
Autocomplete textbox in c# windows application
Reviewed by NEERAJ SRIVASTAVA
on
10:08:00 PM
Rating:
No comments: