I want to implement a toolstripcombobox that that acts like autocompletemode is set to suggest. I didn't set the autocomplete mode since it only finds prefix identical items.
What I want is that it can also find items in the combobox that has a substring even if it doesn't start with that.
sample list:
January, February, March, April, May, June, July, August, September, October, November, December
If I type in the toolstripcombobox for "ber", it should display in the dropdown:
September
October
November
December
respectively.
As of now, I created a separate list that contains the items:
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");
}
and I added a TextUpdate event for the ToolStripCombobox:
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();
}
}
This is my sample implementation. It already searches non-prefix but I'm not quite satisfied with the code since there exists some differences when autocompletemode in suggest is set:
1) When you keypress up or down the drop down for the items, the selectedIndexChanged Event fires unlike the autocompletemode that doesn't
2) And many more minor differences.
What I really want is that It will just imitate the autocomplete mode in suggest but it will search non-prefix-cally..
Any sample codes, links, or suggestions are well appreciated. :)