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.