Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何根据文本框的输入筛选列表框?_C#_.net_Listbox_Filtering - Fatal编程技术网

C# 如何根据文本框的输入筛选列表框?

C# 如何根据文本框的输入筛选列表框?,c#,.net,listbox,filtering,C#,.net,Listbox,Filtering,我是编程新手,我需要关于如何在c应用程序中使用textbox过滤listbox的帮助 我的意思是,在文本框中输入一些文本,同时在列表框中过滤,如果列表中存在项目,则选择进入文本框,否则打开新表单创建项目。请给我一个相同的示例。这是一个简单的自动完成文本框,您应该能够使用它来满足您的需要: class AutoSuggestControl : TextBox { List<string> Suggestions; int PreviousLength; pu

我是编程新手,我需要关于如何在c应用程序中使用textbox过滤listbox的帮助


我的意思是,在文本框中输入一些文本,同时在列表框中过滤,如果列表中存在项目,则选择进入文本框,否则打开新表单创建项目。请给我一个相同的示例。

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

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;
    }

}

控件支持筛选。它还将加粗您尝试过滤的字母。为了避免这种不希望出现的行为,我建议将“粗体”字体改为与普通字体相同。

WinForms、ASP.NET或WPF?无论哪种技术,他都需要一个自动完成的文本框控件。你是说要将文本框中的文本作为新项添加到列表框中吗?嗨,JJ不,先生,我的意思是在文本框中输入一些初始文本,并从列表框中筛选或搜索记录(如果列表框中存在),然后在文本框中选择相同的文本,否则打开新表单以创建新项目。thx sir 4您的反馈Hi sniperx看到我不知道如何根据您建议的代码创建自动完成的文本框我不知道在form1.cs中键入此代码的位置,或者我是新来的位置,请提前指导我thx您需要创建自定义控件。如果创建文件,则不会将其输入到文件中。添加一个文件,比如AutoCompleteTextBox.cs,编译应用程序,然后在工具箱中查找新控件。代码是文档化的,因此需要编辑的部分可以轻松编辑。