我想实现一个工具条组合框,它的作用类似于设置为建议的自动完成模式。我没有设置自动完成模式,因为它只找到前缀相同的项目。
我想要的是它也可以在组合框中找到具有子字符串的项目,即使它不以 that 开头。
样品清单:
一月、二月、三月、四月、五月、六月、七月、八月、九月、十月、十一月、十二月
如果我在工具条组合框中输入"ber",它应该显示在下拉列表中:
九月
十月
十一月
十二月
分别。
截至目前,我创建了一个单独的列表,其中包含以下项目:
void populateList()
{
this->storageList = gcnew Generic::List<String ^>;
storageList->Add("January");
storageList->Add("February");
storageList->Add("March");
storageList->Add("April");
storageList->Add("May");
storageList->Add("June");
storageList->Add("July");
storageList->Add("August");
storageList->Add("September");
storageList->Add("October");
storageList->Add("November");
storageList->Add("December");
}
我为 ToolStripCombobox 添加了一个 TextUpdate 事件:
void handleTextChange()
{
String ^ searchText = toolStripComboBox->Text;
toolStripComboBox->Items->Clear();
Cursor->Current = Cursors::Default;
if(searchText != "")
{
toolStripComboBox->DroppedDown = true;
Regex ^ searchRegex = gcnew Regex("(?i).*"+searchText+".*");
for(int i = 0; i<storageList->Count; i++)
{
Match ^ m = searchRegex->Match(storageList[i]);
if(m->Success)
{
toolStripComboBox->Items->Add(storageList[i]);
}
}
if(toolStripComboBox->Items->Count > 0)
{
String ^ sText = toolStripComboBox->Items[0]->ToString();
toolStripComboBox->SelectionStart = searchText->Length;
toolStripComboBox->SelectionLength = sText->Length - searchText->Length;
}
else
{
toolStripComboBox->DroppedDown = false;
toolStripComboBox->SelectionStart = searchText->Length;
}
}
else
{
toolStripComboBox->DroppedDown = false;
toolStripComboBox->Items->Clear();
}
}
这是我的示例实现。它已经搜索了非前缀,但我对代码不太满意,因为在设置自动完成模式时存在一些差异:
1)当您向上或向下按下项目的下拉菜单时,selectedIndexChanged 事件会触发,这与自动完成模式不同
2)还有更多的细微差别。
我真正想要的是它只会模仿建议中的自动完成模式,但它会搜索非前缀。
非常感谢任何示例代码、链接或建议。:)