【问题标题】:How do I filter a ListBox based on input from a TextBox?如何根据来自 TextBox 的输入过滤 ListBox?
【发布时间】:2011-01-21 23:07:24
【问题描述】:

我是编程新手,我需要有关如何通过在 c# 应用程序中使用文本框来过滤列表框的帮助。

我的意思是在文本框中输入一些文本并在列表框中同时过滤如果项目在列表中退出然后选择到文本框中否则打开新表单以创建项目请给我同样的例子。

【问题讨论】:

  • WinForms、ASP.NET 还是 WPF?
  • 无论哪种技术,他都需要一个自动完成文本框控件。
  • 您的意思是要将文本框中的文本作为新项目添加到列表框中?
  • 嗨 jjj 不先生,我的意思是在文本框中输入一些初始文本并过滤或搜索列表框中的记录,如果它存在于列表框中,然后在文本框中选择相同的内容,否则打开新表单以创建新项目。谢谢先生 4 你的反馈

标签: c# .net listbox filtering


【解决方案1】:

This 控件支持过滤。它还会加粗您尝试过滤的字母。为了解决这个问题,在你的情况下,不受欢迎的行为,我建议将“粗体”字体更改为与普通字体相同。

【讨论】:

    【解决方案2】:

    这是一个简单的自动完成文本框,您应该可以使用它来满足您的需求:

    class AutoSuggestControl : TextBox
    {
        List<string> Suggestions;
        int PreviousLength; 
    
        public AutoSuggestControl() : base()
        {
            Suggestions = new List<string>();
    
            // We keep track of the previous length of the string
            // If the user tries to delete characters we do not interfere
            PreviousLength = 0; 
    
            // Very basic list, too slow to be suitable for systems with many entries
            foreach(var e in yourListbox.Items)
            {
                //add your listbox items to the textbox
            }
            Suggestions.Sort();
        }
    
        /// <summary>
        /// Search through the collection of suggestions for a match
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
    
        private string FindSuggestion(string Input)
        {
            if (Input != "")
            foreach (string Suggestion in Suggestions)
            {
                if (Suggestion.StartsWith(Input))
                    return Suggestion;
            }
            return null;
        }
    
        /// <summary>
        /// We only interfere after receiving the OnTextChanged event.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
    
            // We don't do anything if the user is trying to shorten the sentence
            int CursorPosition = SelectionStart;
            if (Text.Length > PreviousLength && CursorPosition >= 0)
            {
                string Suggestion = FindSuggestion(Text.Substring(0, CursorPosition));
                if (Suggestion != null)
                {
                    // Set the contents of the textbox to the suggestion
                    Text = Suggestion;
                    // Setting text puts the cursor at the beginning of the textbox, so we need to reposition it
                    Select(CursorPosition, 0);
                }
            }
            PreviousLength = Text.Length;
        }
    
    }
    

    【讨论】:

    • 嗨 sniperx 看到我不知道根据您建议的代码创建自动完成的文本框我很困惑在 form1.cs 中输入此代码的位置,或者我是新来的,请指导我提前谢谢
    • 您需要创建一个自定义控件。您不会“将其输入文件”而是创建文件。添加一个文件,例如 AutoCompleteTextBox.cs,编译您的应用程序,然后在工具箱中查找您的新控件。代码已记录在案,因此可以轻松编辑需要编辑的部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 2011-10-28
    • 1970-01-01
    • 2021-12-18
    • 2012-03-09
    • 2022-08-16
    相关资源
    最近更新 更多