Visual Studio 2015工具栏组合,如何正确管理用户输入的值?

Hug*_*iro 7 vsx vspackage vsix vsct visual-studio-2015

我已经使用VSCT文件中的此设置为Visual Studio 2015的VSIX包中的工具栏定义了动态组合:

  <Combo guid="cmdExplorerToolbarSearchGUID" id="cmdExplorerToolbarSearchID" priority="0x0" type="DynamicCombo"
      defaultWidth="50" idCommandList="cmdExplorerToolbarSearchGetListID">
    <Parent guid="grpExplorerToolbar3GUID" id="grpExplorerToolbar3ID" />
    <CommandFlag>DynamicVisibility</CommandFlag>
    <CommandFlag>IconAndText</CommandFlag>
    <CommandFlag>StretchHorizontally</CommandFlag>
    <Strings>
      <CanonicalName>cmdExplorerToolbarSearch</CanonicalName>
      <ButtonText>Search</ButtonText>
      <ToolTipText>Search elements in the model explorer</ToolTipText>
    </Strings>
  </Combo>

</Combos>
Run Code Online (Sandbox Code Playgroud)

相应的DynamicStatusMenuCommand实例定义如下:

    command = new DynamicStatusMenuCommand(
        new EventHandler(this.OnPopUpMenuDisplayAction),
        new EventHandler(this.OnCmdExplorerToolbarSearchSelected),
        new CommandID(CmdExplorerToolbarSearchGUID, CmdExplorerToolbarSearchID));
    commands.Add(command);

    command = new DynamicStatusMenuCommand(
        new EventHandler(this.OnPopUpMenuDisplayAction),
        new EventHandler(this.OnCmdExplorerToolbarSearchGetList),
        new CommandID(CmdExplorerToolbarSearchGUID, CmdExplorerToolbarSearchGetListID));
    commands.Add(command);
Run Code Online (Sandbox Code Playgroud)

最后OnCmdExplorerToolbarSearchSelected像这样的事件处理程序:

private void OnCmdExplorerToolbarSearchSelected(object sender, EventArgs e)
{
    // Process the event arguments

    OleMenuCmdEventArgs args = e as OleMenuCmdEventArgs;
    if (args != null)
    {
        // Process values

        string inValue = args.InValue as string;
        IntPtr outValue = args.OutValue;

        if (outValue != IntPtr.Zero)
        {
            // When outValue is not null, the IDE is requesting the current value for the combo

            Marshal.GetNativeVariantForObject(this.SearchHandler.CurrentValue, outValue);
        }
        else if (inValue != null)
        {
            this.SearchHandler.Search(this.PresentationModel3ExplorerToolWindow.Explorer, inValue);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这导致工具箱中的一个很好的组合:

工具栏的打印屏幕

问题是,如果用户输入"Unit"并按下Enter事件处理程序,则使用inValue!= null调用并执行搜索.但是,如果他输入其他内容(例如:Customer)并按下Tab(否Enter),则组合将恢复为之前的值("Unit"),因为使用args.OutValue!= IntPtr.Zero调用处理程序.

当用户输入内容并将焦点移离组合而不按下时,获取回调的诀窍是什么Enter?并且,鉴于此,如何获得当时组合的值?

Pfh*_*yer 0

我还没有尝试过这个,但是如果您使用OleMenuCommand安装命令,您可以提供一个“Changed”处理程序,似乎只要组合框中的文本发生更改就应该调用它。这可以让你做你想做的事吗?