C# 像 Google 搜索一样搜索组合框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15878862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 18:15:46  来源:igfitidea点击:

Search Combo Box like Google Search

c#winformsautocompletecomboboxsearch-engine

提问by Sam

I am making a Windows Form in that I have A combo box, Into which i have loaded some 'Invoice Numbers', from SQL server 2010. I want to Display Invoice Numbers as the User types into the Combo box. For eg if User types '100' then the Invoice Numbers Starting with '100' should be displayed in the dropdown.

我正在制作一个 Windows 窗体,因为我有一个组合框,我在其中加载了一些来自 SQL Server 2010 的“发票编号”。我想在用户在组合框中键入时显示发票编号。例如,如果用户键入“100”,则应在下拉列表中显示以“100”开头的发票编号。

Please Help, Thanks in Advance...

请帮助,在此先感谢...

采纳答案by Sam

    DataTable temp;
    DataTable bank;
    private void Form1_Load(object sender, EventArgs e)
    {
        comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;

        temp = DbRdRw.SqlDbRead("Select * from BankMaster", "BankMaster");

        DataView dtview = new DataView(temp);
        dtview.Sort = "BankName DESC";
        bank = dtview.ToTable();

        comboBox1.DataSource = bank;
        comboBox1.ValueMember = "BankName";
        comboBox1.DisplayMember = "BankName";
    }

回答by TimWagaman

Try AutoCompleteMode - either Suggest or SuggestAppend depending on the exact behavior you're looking for. Also, remember to set AutoCompleteSource for the list that AutoComplete will base auto completion from (I suggest ListItems).

尝试 AutoCompleteMode - Suggest 或 SuggestAppend,具体取决于您正在寻找的确切行为。另外,请记住为 AutoComplete 将基于自动完成的列表设置 AutoCompleteSource(我建议使用 ListItems)。

http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.autocompletemode.aspx

http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.autocompletemode.aspx

回答by snappieT

What you need to do here is:

您需要在这里做的是:

  • Get an event every time the user types a character in to the text box.
  • Have a function that runs on this event to read the contents of the box ('100' in your example) and fire a query off to your database, for example:

    SELECT InvoiceNumber from Invoices WHERE InvoiceNumber LIKE '100%'

  • Display the matching results back in a combo box for the user to select from.
  • 每次用户在文本框中键入字符时获取一个事件。
  • 有一个在此事件上运行的函数以读取框的内容(在您的示例中为“100”)并向您的数据库发出查询,例如:

    SELECT InvoiceNumber from Invoices WHERE InvoiceNumber LIKE '100%'

  • 将匹配结果显示在组合框中供用户选择。

回答by Drew Kruezer

Fill your combo box with items from database on loading then set Combo box properties:

加载时用数据库中的项目填充组合框,然后设置组合框属性:

AutoCompleteMode: Suggest Append

AutoCompleteMode: 建议追加

AutoCompleteSource: ListItems

AutoCompleteSource: ListItems

Make sure to set the DropDown style to DropDown so user can type in. Just make a validations if inputted text on combo box does exist on the list before accepting.

确保将 DropDown 样式设置为 DropDown 以便用户可以输入。在接受之前,如果组合框中输入的文本确实存在于列表中,则只需进行验证。

hope it helps.

希望能帮助到你。