[SOLVED] My autosuggestion lags

My autosuggestion has been lagging lately and I need your guys help with some code.
I would like to make it that every three characters typed, the autocomplete updates. Here is my current code:

        private void urlTextBox_TextChanged(object sender, EventArgs e)
        {
            try
            {
                aslist.Items.Clear();
                foreach (string item in GComplete.GComplete.GetSuggestions(urlTextBox.Text))
                {
                    aslist.Items.Add(item);

                }

            }
            catch (Exception)
            {

            }

        }

Although this lags when typing fast.Thanks!

A post by brwck

Like 0 Dislike

Hiya @brwck,

That seems like the most common method used for autocompletion in C# nowadays, I've yet to play around and come-up with some faster alternatives. Are you using the built-in autocomplete functionality for the textbox?

Assuming you're fetching your results from the web, you could always speed things up by keeping track of the users' most used search queries and adding them to a smaller list so less time is needed to search through; same can be done for common search queries used globally.

These articles might be of interest:
http://blog.notdot.net/2007/4/Damn-Cool … 1-BK-Trees
http://blog.notdot.net/2010/07/Damn-Coo … n-Automata

Hope this helps! 1f609

Like +1 Dislike

@adminWouldn't it be easier to generate the list every 4 characters after the first letter is typed? Only problem is I don't have a clue on how to do it...

A post by brwck

Like 0 Dislike

Maybe, although the code to check every 4 characters to do so would tend to slow it down around the same amount; pretty much anything you put into the "TextChanged" event will cause a fairly major degradation in typing performance, I suppose you'd just have to find a way to do it with as little code as possible.

Here's my simple solution:

int characterChange = 0;
private void urlTextBox_TextChanged(object sender, EventArgs e)
{
    characterChange++;

    if (characterChange == 4)
    {
        try
        {
            aslist.Items.Clear();
            foreach (string item in GComplete.GComplete.GetSuggestions(urlTextBox.Text))
            {
                aslist.Items.Add(item);
            }
        }
        catch (Exception)
        {
        }
        characterChange = 0;
    }
}

The code doesn't take the removal of characters into account, so it's more like it suggests after 4 keys have been pressed whilst the text box is active.

Like +1 Dislike

@adminMate, you are a lifesaver aha. Problem solved (kinda)

A post by brwck

Like +1 Dislike