DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Automating a Web Form With Playwright MCP and MySQL MCP
  • Moving PeopleSoft ERP Data Between Databases With Data Mover Scripts
  • Understanding Multi-Leader Replication for Distributed Data
  • Fine-Tuning Performance, Resolving Common Issues in FinTech Application With MySQL

Trending

  • Why Your Test Automation Is Always Behind the Code And the Architecture That Fixes It
  • From 24 Hours to 2 Hours: How We Fixed a Broken BI System With Apache Airflow
  • Build a GitHub Slack Bot With AWS Bedrock and MCP, Part 1
  • Securing the AI Host: Spring AI MCP Server Communication With API Keys
  1. DZone
  2. Data Engineering
  3. Databases
  4. AutoCompleteTextBox in C# Windows Form Application

AutoCompleteTextBox in C# Windows Form Application

By 
Anoop Kumar Sharma user avatar
Anoop Kumar Sharma
·
Apr. 23, 15 · Interview
Likes (0)
Comment
Save
Tweet
Share
7.6K Views

Join the DZone community and get the full member experience.

Join For Free

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.

csharp application Form (document) Database

Published at DZone with permission of Anoop Kumar Sharma. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Automating a Web Form With Playwright MCP and MySQL MCP
  • Moving PeopleSoft ERP Data Between Databases With Data Mover Scripts
  • Understanding Multi-Leader Replication for Distributed Data
  • Fine-Tuning Performance, Resolving Common Issues in FinTech Application With MySQL

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook