Saturday, July 2, 2016

Auto Complete TextBox using C#

 Hi in this blog I am going to show how to create a autocomplete textbox in C#.

Output:

Source Code:

//Constructor
 public Mainform()
        {
            InitializeComponent();
            asc = new AutoCompleteStringCollection();
            textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
            textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            addItems_source(asc);
            textBox1.AutoCompleteCustomSource = asc;            
        }

//Add Item Source
 public void addItems_source(AutoCompleteStringCollection col)
        {
            string getlist = File.ReadAllText(Application.StartupPath + "//sample.txt");
            string[] sLabels = getlist.Split('%');
            for (int i = 0; i < sLabels.Length; i++)
            {
                col.Add(sLabels[i]);
            }                
        }
//Button Event Handler
private void button1_Click(object sender, EventArgs e)
        {
            if (asc.Contains(textBox1.Text)) { }
            else
            {
                File.AppendAllText(Application.StartupPath + "//sample.txt", "%" + textBox1.Text);
                addItems_source(asc);
                MessageBox.Show("Files are writtten....");
            }
        }

Download the source code from here.

Click here for my previous Blog.



No comments:

Post a Comment