Home >Backend Development >C++ >How to Implement Custom Autocomplete in C# Using a Custom Event Handler?

How to Implement Custom Autocomplete in C# Using a Custom Event Handler?

Barbara Streisand
Barbara StreisandOriginal
2024-12-31 17:50:10668browse

How to Implement Custom Autocomplete in C# Using a Custom Event Handler?

C# Autocomplete using Custom Event Handler

Problem:

An autocomplete feature is being implemented for a textbox, where results come from a database in the format "[001] Last, First Middle." The current implementation requires typing "[001]" to initiate the search, even though the desired behavior is to allow autocomplete based on typing the first name, e.g., "John" should return "[001] Smith, John D."

Solution:

The built-in AutoComplete functionality in C# only supports prefix-based searching, making it unsuitable for this requirement. A workaround is to create a custom autocomplete function by handling the OnTextChanged event and implementing the desired behavior programmatically.

Implementation:

A ListBox is added below the textbox and its visibility is initially set to false. When the user enters text in the textbox, the OnTextChanged event of the textbox triggers. The handler performs the following steps:

  1. Clears the existing items in the listbox.
  2. If the textbox's text is empty, hides the listbox.
  3. Iterates through the values in the AutoCompleteCustomSource of the textbox.
  4. If a value contains the text entered in the textbox, adds the value to the listbox and sets its visibility to true.

When an item in the listbox is selected, the SelectedIndexChanged event of the listbox is triggered, and the text of the selected item is copied into the textbox's text property. This completes the autocomplete process and hides the listbox.

Code Example:

private void textBox1_TextChanged(object sender, System.EventArgs e)
{
    listBox1.Items.Clear();
    if (textBox1.Text.Length == 0)
    {
        hideResults();
        return;
    }

    foreach (String s in textBox1.AutoCompleteCustomSource)
    {
        if (s.Contains(textBox1.Text))
        {
            Console.WriteLine("Found text in: " + s);
            listBox1.Items.Add(s);
            listBox1.Visible = true;
        }
    }
}

void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
    textBox1.Text = listBox1.Items[listBox1.SelectedIndex].ToString();
    hideResults();
}

void hideResults()
{
    listBox1.Visible = false;
}

This solution provides a rudimentary example of a custom autocomplete functionality that can be further enhanced based on specific requirements. However, it demonstrates the principle of handling text input and updating the list of suggested values dynamically.

The above is the detailed content of How to Implement Custom Autocomplete in C# Using a Custom Event Handler?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn