We use AutoComplete feature in our websites
many times. Wonder, how we can achieve the same in windows forms.
Let us build it today. We will do it step by step as mentioned below
- Open Visual Studio and create a new
Windows Form Application Project and name it "AutoCompleter"
- Drag and Add a TextBox to the form.
- Double Click the form to create the
Form_Load() Event Handler.
private
void Form1_Load(object
sender, EventArgs e)
{
}
- Write the below code in the Event
Handler.
SqlConnection
con = new
SqlConnection(@"Data Source=.\SQLEXPRESS;Initial
Catalog=tempdb;Integrated Security=True;Pooling=False");
con.Open();
SqlCommand
cmd = new
SqlCommand("Select distinct [name] from [IPLTeams]
order by [name] ASC", con);
SqlDataReader
re = cmd.ExecuteReader();
AutoCompleteStringCollection coll = new
AutoCompleteStringCollection();
while(re.Read())
{
coll.Add(re.GetString(0));
}
textBox1.AutoCompleteCustomSource = coll;
con.Close();
- Build the project and Execute.
Hope you find it useful. Let me know your
thoughts via Comments.