在即时搜索算法在列表框中 [英] on-the-fly search algorithm in listbox

查看:102
本文介绍了在即时搜索算法在列表框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有项目x个列表框。在列表框顶部我有一个文本框(这是搜索领域)。我尝试做开发了一种算法,从列表框中删除的项目,如果它不包含搜索内容(代码中的变量关键字)。这应该发生的每个键的用户类型(在即时)。因此,代码:

So, I have a listbox with x number of items. On top of the listbox I have a TextBox (this is the search field). I try do develop an algorithm that removes items from the listbox, if it doesn't contain the searchword (variable keyword in the code). This is supposed to happen for each key the user types (on-the-fly). So, the code:

   private void _keywordTextBox_TextChanged(object sender, EventArgs e)
    {
        string keyword = _keywordTextBox.Text;

        if (keyword == searchtext || isSpace) // do nothing if space is typed - searchtext is a templatetext in the textbox ("type here to search...")
            return; // ignore
        else if (keyword == "")
        {
            listBox.Items.Clear();

            foreach (string s in originalList)
                listBox.Items.Add(s);
        }
        else
        {
            List<string> selection = new List<string>();

            foreach (string s in originalList) // originalList is the listbox at startup
                selection.Add(s);

            listBox.BeginUpdate();
            string[] keywordSplit = keyword.Split(' ');

            try
            {
                for (int i = originalList.Count - 1; i >= 0; i--)
                {
                    string[] selectionSplit = selection[i].Split(' ');

                    int l = 0; // number of hits

                    for (int j = 0; j < selectionSplit.Length; j++)
                    {
                        for (int k = 0; k < keywordSplit.Length; k++)
                        {
                            if (selectionSplit[j].ToLower().Contains(keywordSplit[k].ToLower()))
                            {
                                l++;
                                break;
                            }
                        }
                    }

                    if (l < keywordSplit.Length) // Not hit on all keywords
                        selection.RemoveAt(i);
                }
            }
            finally
            {
                listBox.Items.Clear();

                foreach (string s in selection) // Add selection in listbox
                    listBox.Items.Add(s);

                if (listBox.Items.Count > 0)
                    listBox.SetSelected(0, true); // Select first item in listbox

                listBox.EndUpdate();
            }
        }
    }

问题是一言难尽,比它的其他按预期不工作。该水煤浆是,据我所看到的,零星的。

The problem is hard to describe, other than it doesn't work as intended. The behavour is, as far as I can see, sporadic.

如果我搜索CK流,我应该得到计算器一击。更重要的是,它也应该,如果我删除字符(退格删除关键的)工作。 ?有人

If I search for "ck flow", I should get a hit for stackoverflow. More importantly, it should also work if I deletes chars (delete key of backspace). Anybody?

编辑:更多详细信息:

列表框应该收缩和增长的每次击键,依据是什么用户搜索。列表框应该保持每个用户键入的关键字匹配项,过滤掉不匹配。

The listbox should shrink and grow on each keystroke, based on what the user searches for. The listbox should keep every item that matches the keyword typed in by the user, and filter away that doesn't match.

推荐答案

或者你可以尝试制定出一个正则表达式:

Or you could try to work out a Regular Expression:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    string keyword = textBox1.Text;
    if (string.IsNullOrEmpty(keyword.Trim()))
    {
        listBox1.Items.Clear();
        listBox1.Items.AddRange(_originalList.ToArray());
    }
    else
    {
        Regex regex = new Regex(GetRegexPatternFromKeyword(keyword));
        List<string> selection =
            _originalList.Where(s => regex.IsMatch(s)).ToList();
        listBox1.Items.Clear();
        listBox1.Items.AddRange(selection.ToArray());
    }
}

private static string GetRegexPatternFromKeyword(string keyword)
{
    string[] words =
        keyword.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(word => "(?=.*" + word.Replace(")", @"\)") + ")").ToArray();
    return string.Join("", words);
}



免责声明:可能会有一些情况下,一个输入将摧毁的正则表达式

disclaimer: there could be some cases where an input would 'destroy' the regex pattern

这篇关于在即时搜索算法在列表框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆