Creating a Google Suggestish Text Box in C# WinForms

Have you ever wanted to implement a smart text box in your C# WinForms application that recalls user entries just like Google? This functionality, often referred to as an autocomplete feature, not only enhances user experience but also makes your application more adaptable to user behavior. In this blog post, we will walk through how to create a Google Suggestish text box that remembers the last x number of entries, ensuring efficiency and convenience for your users.

The Problem

When developing a standalone application in C#, finding an effective way to constantly remember user inputs can be challenging. Users expect a seamless experience where their recent inputs are available for quick selection, especially in forms or search fields. The objective is clear: create a text box that retains the last few entries even after closing the application.

The Solution

The good news is that implementing this feature is quite straightforward! We will use AutoCompleteStringCollection from the System.Windows.Forms namespace to manage our autocomplete feature. Moreover, we will save the input to a text file to make the entries persistent across sessions.

Step-by-Step Guide

Let’s break down the implementation into manageable parts.

1. Setting Up the Form

First, you need to create a simple WinForms application with a TextBox. The TextBox will need to be configured to use a custom autocomplete source.

namespace AutoComplete
{
    public partial class Main : Form
    {
        // Creating a new instance of the AutoCompleteStringCollection
        AutoCompleteStringCollection acsc;

        public Main()
        {
            InitializeComponent();

            // Configure the TextBox
            txtMain.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtMain.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

            // Initialize the AutoCompleteStringCollection
            acsc = new AutoCompleteStringCollection();
            txtMain.AutoCompleteCustomSource = acsc;
        }
    }
}

2. Handling User Input

Next, we need to track user inputs. We can do this by responding to the KeyDown event when the Enter key is pressed. Here’s how you can manage the entries:

private void txtMain_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        if (acsc.Count < 10)
        {
            // Add the current text to the collection
            acsc.Add(txtMain.Text);
        }
        else
        {
            // Remove the oldest entry and add the new one
            acsc.RemoveAt(0);
            acsc.Add(txtMain.Text);
        }
    }
}

3. Saving and Loading Entries

To ensure that the entries are not lost when the application is closed, we need to implement saving and loading mechanisms.

  • Saving Entries: This occurs in the Main_FormClosed event where we write the entries to a file.
private void Main_FormClosed(object sender, FormClosedEventArgs e)
{
    using (StreamWriter sw = new StreamWriter("AutoComplete.acs"))
    {
        foreach (string s in acsc)
            sw.WriteLine(s);
    }
}
  • Loading Entries: Implement this in the Main_Load event to populate the text box with previous entries stored in the file.
private void Main_Load(object sender, EventArgs e)
{
    if (File.Exists("AutoComplete.acs"))
    {
        using (StreamReader sr = new StreamReader("AutoComplete.acs"))
        {
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                acsc.Add(line);
            }
        }
    }
}

Conclusion

By following these steps, you can create a text box in your C# application that remembers the last few user inputs, offering a user-friendly experience reminiscent of Google’s autocomplete feature.

This guide illustrates a basic implementation but feel free to modify the logic to create a more sophisticated model according to your needs. Whether incorporating a rating system for entries or customizing entry limits, the possibilities for enhancement are endless.

If you have any questions or ideas to share regarding your implementation, please leave a comment below!