本文介绍了ToolStripComboBox控件的自定义自动搜索在Visual C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个ToolStripComboBox控件,像autocompletemode设置为提示作用。
我没有设置自动完成模式,因为它只能找到preFIX相同的项目。

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.

我要的是,它也可以查找中,有一个子,即使它不与启动组合框项目

一月,二月,三月,四月,五月,六月,七月,八月,九月,十月,十一月,十二月

如果我在ToolStripComboBox控件符合BER键入,它应该在下拉列表中显示:

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

九月十月
十一月

分别。

截至目前,我创建了一个包含的项目一个单独的列表:

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");
}

和我添加了一个ToolStripComboBox控件事件TextUpdate:

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();
        }
      }

这是我的示例实现。它已经将搜索非preFIX,但我不是很满意,code,因为当autocompletemode在提示设置存在一定的差异:

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)如果您键preSS向上或向下下拉的项目,不像autocompletemode SelectedIndexChanged事件火灾不

2)还有更多细微的差别。

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.

我真正想要的是,这将只是模仿的自动完成模式的建议,但它会搜索非preFIX - 凯莉..

What I really want is that It will just imitate the autocomplete mode in suggest but it will search non-prefix-cally..

任何样品codeS,链接,或建议,以及AP preciated。 :)

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

推荐答案

通过这种间接但有效样本的帮助后,我终于能够解决这个问题。


Override组合框的WinForms自动完成建议规则




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

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

的SelectedIndexChanged 的问题终于解决了这个:

Stop从加载窗体时的射击和
组合框的SelectedIndexChanged事件
The是的SelectedIndexChanged触发自动不选择在C#Windows应用程序

and with the problem of selectedIndexChanged was solved by this:
Stop comboBox's selectedIndexChanged event from firing when the form loads and
The selectedIndexChanged is auto triggered without selecting items in combobox in c# windows application

要总括起来,以创造一个仿 autocompletemode在提示这不只是prefixed搜索的基础,你需要订阅数事件的 ToolStripComboBox控件组合框

To sum it all up, in order to create an imitation of autocompletemode in suggest that dont just base on prefixed searches, you need to subscribe to several events of ToolStripComboBox and ComboBox.

下面是您需要创建和修改的事件:

Here are the events that you need to create and modify:

toolStripComboBox_TextUpdate
toolStripComboBox_KeyDown
toolStripComboBox_DropDown
toolStripComboBox_ChangeCommit



在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总结:此事件刚刚处理的匹配和ToolStripComboBox控件的人口和下降的状态下

TextUpdate summary: this event just handles the matching and population of the toolStripComboBox and the status of the drop down



在的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摘要:这个处理是pssed像向上和向下箭头,退格$ P $特殊键和回车键..注意到事件ChangeCommit()被触发你的时候preSS进入。出现这种情况,因为当你preSS进入ChangeCommit事件不火​​,只能通过鼠标点击。

KeyDown Summary: this handles the special keys that are pressed like the up and down arrow, backspace, and the enter key.. notice that the event ChangeCommit() was triggered when you press enter. this happens because ChangeCommit event does not fire when you press enter, only by mouse click.



在下拉():

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

下拉摘要:它只是在 DroppedDown 属性设置为正确的,因为一个小的修复时,prevents的编辑部分 ToolStripComboBox控件来选择列表中的第一个项目。

DropDown Summary: its just a minor fix since when the DroppedDown property is set to true, it prevents the editable part of the ToolStripComboBox to select the first item on the list.



在ChangeCommit():
因为我有这样一个问题:

1)当你键preSS向上或向下下拉的项目,不像autocompletemode SelectedIndexChanged事件火灾不

解决这个问题的解决方案是从的SelectedIndexChanged 来退订和事件处理程序 ChangeCommit 这是不是在方法取代它在 ToolStripComboBox控件不过是组合框的方法:


On ChangeCommit():Since I have a problem like this:
1) When you keypress up or down the drop down for the items, the selectedIndexChanged Event fires unlike the autocompletemode that doesn't
The solution to this is to unsubscribe from the SelectedIndexChanged and replace it with an event handler ChangeCommit which is not a method in the ToolStripComboBox but is a method of the ComboBox:

这个 - &GT; toolStripComboBox-&GT; ComboBox-&GT; SelectionChangeCommitted + = gcnew系统:EventHandler的(这一点,和放大器; Form1中:: toolStripComboBox_ChangeCommit);

此毒手实施,YEEEY后!我的成功模仿 autocompletemode提示与文字匹配的只有一个子项建议.. !!

这个问题也可以是简单的解决方案组合框

这种方法可能会有点脏,所以其他人可以只是继承和覆盖事件,以便它可以使code整洁。

After this murderous implementation, YEEEY! I've successfully imitated the autocompletemode suggest with suggesting items matching only a substring in the text..!!
This problem can also be a solution for Simple ComboBoxes.
This method may be a little bit dirty, so others can just inherit and override the events so it can make the code neater.

这篇关于ToolStripComboBox控件的自定义自动搜索在Visual C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 15:01