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

7

我已经在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>

相应的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);

最后,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);
        }
    }
}

这将在工具箱中产生一个好的组合:

打印工具栏截图

问题在于,如果用户输入“Unit”并按下Enter,事件处理程序将使用inValue != null进行调用,并执行搜索。但是,如果他随后输入其他内容(例如:Customer)并按下Tab(没有Enter键),则组合框会恢复到先前的值(“Unit”),因为使用args.OutValue != IntPtr.Zero调用了处理程序。

如何在用户输入内容并将焦点移开组合框而不按下Enter时获得回调?有了这个,我该怎么样获取此时在组合框中的值?

1个回答

0

我没试过,但如果你使用OleMenuCommand安装你的命令,你可以提供一个"Changed"处理程序,看起来应该在组合框中的文本更改时被调用。这可能可以让你实现你想要的功能?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接