Individual autostart ToolStripCombobox in Visual C ++

I want to implement a toolstripcombobox for which autocompletemode acts. I did not set the autocomplete mode, since it only finds the prefix of identical elements.

I want it to also be able to find elements in combobox that have a substring, even if it does not start with this .

list of examples:

January, February, March, April, May, June, July, August, September, October, November, December

If I type toolstripcombobox for "ber" , it should appear in the dropdown menu:

September
October
November
December

respectively.

At the moment, I have created a separate list containing the elements:

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 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 is not looking for a prefix already, but I'm not quite happy with the code, since there is a certain difference when installing autocompletemode in the tooltip:

1) When you press the up or down key for items, the selectedIndexChanged event fires, unlike autocomplete, which does not work
2) And many other minor differences.

I really want it to just mimic the autocomplete mode in the tooltip, but it will not look for the prefix-cally ..

Any examples of codes, links or suggestions are well appreciated. :)

0
autocomplete visual-c ++ winforms toolstripcombobox
Dec 19 '14 at 4:50
source share
1 answer

With the help of these indirect but useful patterns, I was finally able to solve this problem.

Override Winforms ComboBox Autocomplete Suggest a Rule
http://www.codeproject.com/Articles/3958/AC-auto-complete-combo-box
http://www.codeproject.com/Tips/631196/ComboBox-with-Suggest-Ability-based-on-Substring-S

and with the selectedIndexChanged problem was solved as follows:
Stop the selectedIndexChanged event from comboBox when firing when the form loads and
Selected optionIndexChanged starts automatically without selecting items in combobox in C # windows application

To summarize, to create an imitation of an autocompletemode in a suggestion that is not based on preliminary search attempts, you need to subscribe to several ToolStripComboBox and ComboBox events.

The following are the events that need to be created and modified:

 toolStripComboBox_TextUpdate toolStripComboBox_KeyDown toolStripComboBox_DropDown toolStripComboBox_ChangeCommit 


In TextUpdate ():

 toolStripComboBox_TextUpdate(System::Object^ sender, System::EventArgs^ e) { String ^ searchText = toolStripComboBox->Text; toolStripComboBox->Items->Clear(); if(searchText != "") { Regex ^ searchRegex = gcnew Regex("(?i).*"+searchText+".*"); for(int i = 0; i<storageList->Count; i++) { Match ^ m = searchRegex->Match(storageList[i]); if(m->Value == storageList[i]) { toolStripComboBox->Items->Add(storageList[i]); } } if(toolStripComboBox->Items->Count > 0) { toolStripComboBox->DroppedDown = true; toolStripComboBox->Text = searchText; Cursor->Current = Cursors::Default; } else { toolStripComboBox->DroppedDown = false; } toolStripComboBox->SelectionStart = searchText->Length; } else { toolStripComboBox->DroppedDown = false; toolStripComboBox->Items->Clear(); } } 

TextUpdate Summary: This event handles a match and a combination of toolStripComboBox and dropdown status.


In KeyDown ():

 toolStripComboBox_KeyDown(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e) { String ^ searchText = toolStripComboBox->Text; if(e->KeyCode == Keys::Down || e->KeyCode == Keys::Up) { if(e->KeyCode == Keys::Down) { if(toolStripComboBox->SelectedIndex == -1 && toolStripComboBox->Items->Count > 0) { toolStripComboBox->SelectedIndex = 0; } } if(e->KeyCode == Keys::Up) { if(toolStripComboBox->SelectedIndex == -1 && toolStripComboBox->Items->Count > 0) { toolStripComboBox->SelectedIndex = toolStripComboBox->Items->Count - 1; } } keydownTxt->Text = Convert::ToString(toolStripComboBox->SelectedIndex); } if(e->KeyCode == Keys::Back) { toolStripComboBox->SelectionStart = searchText->Length; } if(e->KeyCode == Keys::Enter) { toolStripComboBox_ChangeCommit(sender, e); } } 

KeyDown Summary: handles special keys that are pressed like the up and down arrows, backspace, and the enter key. Note that the ChangeCommit () event was raised when the enter key was pressed. this is because the ChangeCommit event does not fire when you press the enter key, only with a mouse click.


In DropDown ():

 toolStripComboBox_DropDown(System::Object^ sender, System::EventArgs^ e) { String ^ searchText = toolStripComboBox->Text; toolStripComboBox->SelectionStart = searchText->Length; } 

Short description of DropDown: its just a small correction, because when the DroppedDown property DroppedDown set to true , it does not allow the edited part of ToolStripComboBox to select the first item in the list.


In ChangeCommit (): Since I have a problem like this:
1) When you click up or down on the drop-down icon for items, the selectedIndexChanged event fires, unlike autocompletion, which does not work
The solution to this is to unsubscribe from selectedIndexChanged and replace it with the ChangeCommit event ChangeCommit , which is not a method in ToolStripComboBox , but a ComboBox method:

this->toolStripComboBox->ComboBox->SelectionChangeCommitted += gcnew System::EventHandler(this, &Form1::toolStripComboBox_ChangeCommit);

After this killer YEEEY implementation! I have successfully simulated autocompletemode, suggesting , suggesting elements matching only a substring in the text .. !!
This issue may also be a solution for Simple ComboBoxes .
This method can be a little dirty, so others can simply inherit and redefine events so that it can make the code tidier.

+1
Dec 23 '14 at 2:57
source share



All Articles