c# - How to make an auto-complete textbox in a winforms desktop application

C# - How to make an auto-complete textbox in a winforms desktop application

Creating an auto-complete textbox in a WinForms desktop application in C# involves using the TextBox control along with a data source to provide suggestions as the user types. Here's a step-by-step guide on how to implement it:

  1. Add a TextBox control to your form: Drag and drop a TextBox control from the Toolbox onto your WinForms form.

  2. Set up the auto-complete functionality: In the form's constructor or Load event handler, configure the auto-complete behavior of the TextBox control.

public Form1() { InitializeComponent(); // Call a method to set up auto-complete SetupAutoComplete(); } private void SetupAutoComplete() { // Create a list of suggestions (can be fetched from a database, file, etc.) string[] suggestions = { "Apple", "Banana", "Orange", "Pineapple", "Mango" }; // Create an AutoCompleteStringCollection from the list of suggestions AutoCompleteStringCollection autoCompleteCollection = new AutoCompleteStringCollection(); autoCompleteCollection.AddRange(suggestions); // Set up auto-complete behavior for the TextBox textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend; textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource; textBox1.AutoCompleteCustomSource = autoCompleteCollection; } 

In this example, the SetupAutoComplete method sets up the auto-complete behavior for the TextBox by configuring its AutoCompleteMode, AutoCompleteSource, and AutoCompleteCustomSource properties. It uses a hard-coded list of suggestions, but you can fetch suggestions from any data source like a database or a file.

  1. Handle selection from the auto-complete dropdown (optional): If you want to perform an action when the user selects an item from the auto-complete dropdown, you can handle the KeyDown or KeyPress event of the TextBox.
private void textBox1_KeyDown(object sender, KeyEventArgs e) { // Check if the Enter key is pressed if (e.KeyCode == Keys.Enter) { // Perform an action when the Enter key is pressed (e.g., display selected item) MessageBox.Show("Selected item: " + textBox1.Text); } } 

In this example, the KeyDown event handler checks if the Enter key is pressed and performs an action (displaying a message box with the selected item) when it is.

With these steps, you can create an auto-complete textbox in your WinForms application that suggests items as the user types. Adjust the suggestions and the action performed based on your application's requirements.

Examples

  1. "C# WinForms autocomplete textbox example"

    Description: Developers often seek examples of implementing auto-complete functionality in a textbox within a WinForms desktop application. This query aims to find code snippets or tutorials demonstrating how to achieve this.

    using System; using System.Windows.Forms; public class AutoCompleteTextBoxExample : Form { private TextBox textBox; public AutoCompleteTextBoxExample() { textBox = new TextBox(); textBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend; textBox.AutoCompleteSource = AutoCompleteSource.CustomSource; AutoCompleteStringCollection autoCompleteCollection = new AutoCompleteStringCollection(); autoCompleteCollection.AddRange(new string[] { "Apple", "Banana", "Orange", "Mango" }); textBox.AutoCompleteCustomSource = autoCompleteCollection; Controls.Add(textBox); } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new AutoCompleteTextBoxExample()); } } 
  2. "C# WinForms textbox auto-complete from database"

    Description: Developers may want to populate auto-complete suggestions in a textbox from a database in a WinForms application. This query seeks solutions or examples demonstrating how to fetch data from a database and bind it to the textbox for auto-completion.

    using System; using System.Windows.Forms; using System.Data.SqlClient; public class DatabaseAutoCompleteTextBox : Form { private TextBox textBox; public DatabaseAutoCompleteTextBox() { textBox = new TextBox(); textBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend; textBox.AutoCompleteSource = AutoCompleteSource.CustomSource; AutoCompleteStringCollection autoCompleteCollection = new AutoCompleteStringCollection(); using (SqlConnection connection = new SqlConnection("YourConnectionString")) { string query = "SELECT Name FROM YourTable"; SqlCommand command = new SqlCommand(query, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { autoCompleteCollection.Add(reader["Name"].ToString()); } } textBox.AutoCompleteCustomSource = autoCompleteCollection; Controls.Add(textBox); } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new DatabaseAutoCompleteTextBox()); } } 
  3. "C# WinForms textbox auto-complete from list"

    Description: Developers may seek methods to provide auto-complete suggestions in a WinForms textbox from a predefined list. This query aims to find examples or guidance on how to achieve auto-completion using a list of items.

    using System; using System.Windows.Forms; using System.Collections.Generic; public class ListAutoCompleteTextBox : Form { private TextBox textBox; public ListAutoCompleteTextBox() { textBox = new TextBox(); textBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend; textBox.AutoCompleteSource = AutoCompleteSource.CustomSource; List<string> itemList = new List<string> { "Apple", "Banana", "Orange", "Mango" }; AutoCompleteStringCollection autoCompleteCollection = new AutoCompleteStringCollection(); autoCompleteCollection.AddRange(itemList.ToArray()); textBox.AutoCompleteCustomSource = autoCompleteCollection; Controls.Add(textBox); } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new ListAutoCompleteTextBox()); } } 
  4. "C# WinForms textbox auto-complete based on user input"

    Description: This query targets developers looking for ways to provide auto-complete suggestions in a WinForms textbox based on user input. Implementing this functionality enhances user experience by suggesting relevant options as the user types.

    using System; using System.Windows.Forms; public class UserInputAutoCompleteTextBox : Form { private TextBox textBox; public UserInputAutoCompleteTextBox() { textBox = new TextBox(); textBox.AutoCompleteMode = AutoCompleteMode.Suggest; textBox.AutoCompleteSource = AutoCompleteSource.CustomSource; textBox.TextChanged += TextBox_TextChanged; Controls.Add(textBox); } private void TextBox_TextChanged(object sender, EventArgs e) { // Implement logic to fetch auto-complete suggestions based on user input // Update AutoCompleteCustomSource accordingly } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new UserInputAutoCompleteTextBox()); } } 
  5. "C# WinForms textbox auto-complete with custom data source"

    Description: Developers may want to use a custom data source for providing auto-complete suggestions in a WinForms textbox. This query aims to find examples or guidance on how to implement auto-completion using a custom data source.

    using System; using System.Windows.Forms; public class CustomDataSourceAutoCompleteTextBox : Form { private TextBox textBox; public CustomDataSourceAutoCompleteTextBox() { textBox = new TextBox(); textBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend; textBox.AutoCompleteSource = AutoCompleteSource.CustomSource; // Implement custom data source for auto-complete suggestions // Assign to AutoCompleteCustomSource property Controls.Add(textBox); } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new CustomDataSourceAutoCompleteTextBox()); } } 
  6. "C# WinForms textbox auto-complete with delay"

    Description: Developers may seek ways to introduce a delay before providing auto-complete suggestions in a WinForms textbox. Implementing a delay can help optimize performance and prevent unnecessary suggestions while typing.

    using System; using System.Windows.Forms; using System.Threading; public class DelayedAutoCompleteTextBox : Form { private TextBox textBox; private Timer timer; public DelayedAutoCompleteTextBox() { textBox = new TextBox(); textBox.AutoCompleteMode = AutoCompleteMode.Suggest; textBox.AutoCompleteSource = AutoCompleteSource.CustomSource; textBox.TextChanged += TextBox_TextChanged; timer = new Timer(TimerCallback, null, Timeout.Infinite, Timeout.Infinite); Controls.Add(textBox); } private void TextBox_TextChanged(object sender, EventArgs e) { timer.Change(500, Timeout.Infinite); } private void TimerCallback(object state) { // Implement logic to fetch auto-complete suggestions // Update AutoCompleteCustomSource accordingly } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new DelayedAutoCompleteTextBox()); } } 

More Tags

line-numbers cycle mkdir text-parsing flutter-packages sequence ord friendly-url background-drawable url.action

More Programming Questions

More Math Calculators

More Statistics Calculators

More Gardening and crops Calculators

More Auto Calculators