13 June 2014

Auto Complete TextBox in Window Form C#

Hi All,

Let see how to create the AutoComplete TextBox in window form.
You might be aware of Auto Complete in web page using the Ajax and JQuery plugins or using the asp.net ajax toolkit.

But when it comes to the Auto Complete TextBox in Window Form some people find it difficult.

I am gonna demonstrate you Auto Complete Textbox for months Jan - Dec.

Download Sample
  • Open Visual Studio and create a Window Form application. 
  • It will add a blank default Window Form for you.
  • Drag a label name it Months and a Input TexBox for Auto Complete Months.

  • Double Click form to open Form_Load event. and create AutoCompleteStringCollection class object.
  • Add you indented string item in object in this case months name string array.
        private void Form1_Load(object sender, EventArgs e)
        {            
            // Create the list to use as the custom source.  
            var source = new AutoCompleteStringCollection();            
            source.AddRange(new string[]
                    {
                        "January",
                        "February",
                        "March",
                        "April",
                        "May",
                        "June",
                        "July",
                        "August",
                        "September",
                        "October",
                        "November",
                        "December"
                    });
            
            // attched it with the created text box.
            textBox1.AutoCompleteCustomSource = source;
            textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
        }

  • Bind the AutoCompleteStringCollection with textBox1.

and you are done.  Now when you run the form and just type only one character "j" it will show the months starting from "j".

Happy knowledge sharing.. ☺

No comments:

Post a Comment