簡體   English   中英

如何在用戶在c#中的組合框內鍵入拼寫時創建組合框自動填充

[英]How to Create combo box to auto fill while user type the spelings inside the combo Box in c#

我的朋友們,我在windows窗體中有一個組合框,我可以用數據庫中的數據填充它,但是當用戶在組合框中鍵入字母時,我無法填充組合框。例如,當用戶鍵入字母“R”時“在組合框和組合框的旁邊必須下降並顯示所有可能的字母”R“

  1. yourComboBox.AutoCompleteSource設置為AutoCompleteSource.ListItems; (如果你的yourComboBox.Items已經從數據庫填充)
  2. yourComboBox.AutoCompleteMode設置為SuggestAppend

您必須使用comboBox上的KeyUp事件,並使用comboBox.Text過濾comboBox.Items集合以僅顯示包含的類型字符。 您還需要強制comboBox窗口下拉。

希望這可以幫助你:

private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    char ch = e.KeyChar;
    string strToFind;

    // if first char
    if (lastChar == 0)
        strToFind = ch.ToString();
    else
        strToFind = lastChar.ToString() + ch;

    // set first char
    lastChar = ch;

    // find first item that exactly like strToFind
    int idx = comboBox1.FindStringExact(strToFind);

    // if not found, find first item that start with strToFind
    if (idx == -1) idx = comboBox1.FindString(strToFind);

    if (idx == -1) return;

    comboBox1.SelectedIndex = idx;

    e.Handled = true;
}

void comboBox1_GotFocus(object sender, EventArgs e)
{
    // remove last char before select new item
    lastChar = (char) 0;
}

這里開始

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM