由於您已更新您的問題,我更瞭解您的問題。你也說過底層的數據和功能是不相關的,這使得你很難準確理解你想要達到的目標,所以我的建議是創建一個自定義的ComboBox
,看看你是否可以處理你的匹配擁有。
我覺得最優雅的方式來編寫函數來測試鍵入的文本是否是
ComboBox
中的項目將使用擴展方法。你的電話是這樣的:可以創建
// see if the Text typed in the combobox is in the autocomplete list
bool bFoundAuto = autoCompleteCombo.TextIsAutocompleteItem();
// see if the Text type in the combobox is an item in the items list
bool bFoundItem = autoCompleteCombo.TextIsItem();
擴展方法如下,你可以自定義搜索的邏輯究竟是如何工作的。在下面我寫的兩種擴展方法中,他們只是檢查在AutoCompleteCustomSource
集合中是否找到鍵入到ComboBox
中的文本,或者在第二個函數中是否在Items
集合中找到文本。
public static class MyExtensions
{
// returns true if the Text property value is found in the
// AutoCompleteCustomSource collection
public static bool TextIsAutocompleteItem(this ComboBox cb)
{
return cb.AutoCompleteCustomSource.OfType<string>()
.Where(a => a.ToLower() == cb.Text.ToLower()).Any();
}
// returns true of the Text property value is found in the Items col
public static bool TextIsItem(this ComboBox cb)
{
return cb.Items.OfType<string>()
.Where(a => a.ToLower() == cb.Text.ToLower()).Any();
}
}
我在哪裏可以使用它?你能指導我多一點嗎?第二個代碼是方法。好吧,第一個代碼應該寫在哪裏? – Diego 2012-04-24 11:24:55
您可以將MyExtensions類放置在您的項目中的自己的文件@Diego中。只要將它放在與表單相同的名稱空間中。然後,在代碼中需要檢查輸入的文本是否是組合框中的項目的地方,您可以調用'autoCompleteCombo.TextIsItem();'。 – 2012-04-24 14:39:09
我認爲我們有一個錯誤的理解。我想要做的是具有AutoComplete的所有功能,但是當它(來自ComboBox的代碼)搜索哪些項匹配時,它會使用自定義比較器。 – Diego 2012-04-24 14:47:24