AutoComplete Feature in C#


This is a very useful feature for any graphical user interface which makes it easy for users to fill in applications or forms by suggesting them suitable words or phrases in appropriate text boxes. So, while it may look like a tough job; its actually quite easy to use this feature. The text boxes in C Sharp contain an AutoCompleteMode which can be set to one of the three available choices i.e. Suggest , Append or SuggestAppend. Any choice would do but my favourite is the SuggestAppend. In SuggestAppend, Partial entries make intelligent guesses if the ‘So Far’ typed prefix exists in the list items. Next we must set the AutoCompleteSource to CustomSource as  we will supply the words or phrases to be suggested through a suitable data source. The last step includes calling the AutoCompleteCustomSouce.Add() function with the required. This function can be used at runtime to add list items in the box.



using System;

namespace Auto_Complete_Feature_in_C_Sharp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox2.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

            textBox2.AutoCompleteSource = AutoCompleteSource.CustomSource;

            textBox2.AutoCompleteCustomSource.Add("code-kings.blogspot.com");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox2.AutoCompleteCustomSource.Add(textBox1.Text.ToString());

            textBox1.Clear();
        }
    }
}



** Do not Copy & Paste code written here ; instead type it in your Development Environment
** Testing done in .Net Framework 2.0 but code should be very similar for subsequent versions of .Net
** All Program Codes on Blog are  100%  tested & running.
Custom Search