Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

AutoCompleteTextBox in C# Windows Form Application

DZone's Guide to

AutoCompleteTextBox in C# Windows Form Application

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

In this Article, We will learn how to create AutoCompleteTextBox using C# Windows Form Application. In my previous article, we learned How to Search Records in DataGridView Using C#.

Let's Begin.

Create a new Windows Form Application.

Drop a Label and TextBox Control from the ToolBox.

Now go to Code behind file(.cs code) and add the following Code:


 using System;
using System.Windows.Forms;

namespace AutoCompleteTextBoxDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //AutoCompleteData Method
        private void autoCompleteData()
        {
            //Set AutoCompleteSource property of txt_StateName as CustomSource
            txt_StateName.AutoCompleteSource = AutoCompleteSource.CustomSource;
            //Set AutoCompleteMode property of txt_StateName as SuggestAppend. SuggestAppend Applies both Suggest and Append
            txt_StateName.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            txt_StateName.AutoCompleteCustomSource.AddRange(new string[]{"Maharastra","Andhra Pradesh","Assam","Punjab","Arunachal Pradesh","Bihar","Goa","Gujarat","Haryana"});
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            autoCompleteData();
        }
    }
}

In the preeceding code, We set the AutoCompleteSource, AutoCompleteMode and AutoCompleteCustomSource properties of Textbox named as txt_StateName so that it automatically completes the input string.

Preview:


AutoComplete TextBox using a Database:

In this example, We will Suggest/Append the data in TextBox(txt_StateName) from the Database. For Demonstration, I have created a Database (named Sample). Add a Table, tbl_State. The following is the table schema for creating tbl_State.

Add the following lines of code:


 using System;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace AutoCompleteTextBoxDemo
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        private void autoCompleteData()
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Sample;Integrated Security=true;");
            SqlCommand com = new SqlCommand("Select State from tbl_State", con);
            con.Open();
            SqlDataReader rdr = com.ExecuteReader();
            //AutoCompleteStringCollection Contains a collection of strings to use for the auto-complete feature on certain Windows Forms controls.
            AutoCompleteStringCollection autoCompleteCollection = new AutoCompleteStringCollection();
            while (rdr.Read())
            {
                autoCompleteCollection.Add(rdr.GetString(0));
            }
            //Set AutoCompleteSource property of txt_StateName as CustomSource
            txt_StateName.AutoCompleteSource = AutoCompleteSource.CustomSource;
            //Set AutoCompleteMode property of txt_StateName as SuggestAppend. SuggestAppend Applies both Suggest and Append
            txt_StateName.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            txt_StateName.AutoCompleteCustomSource = autoCompleteCollection;
            con.Close();
        }
        //Form2_Load Event
        private void Form2_Load(object sender, EventArgs e)
        {
            autoCompleteData();
        }
    }
}
Preview:

Hope you like it. Thanks.

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}