Visual Studio 2015 toolbar combo, how to manage the value entered by the user correctly?

488 views Asked by At

I have defined a dynamic combo for a toolbar in a VSIX package for Visual Studio 2015 using this setting in the VSCT file:

  <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>

The corresponding DynamicStatusMenuCommand instances are defined as follow:

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

And finally the OnCmdExplorerToolbarSearchSelected event handler like this:

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

This results in a nice combo in the toolbox:

Print-screen of the toolbar

The problem is that if the user, for example, enters "Unit" and presses Enter the event handler is called with inValue != null and the search is performed. But if, then, he enters something else (eg: Customer) and presses Tab (no Enter), the combo reverts back to the previous value ("Unit") because the handler is called with args.OutValue != IntPtr.Zero.

What is the trick to get a callback when the user enters something and moves the focus away from the combo without pressing Enter? And, given that, how can I get the value that is on the combo at that moment?

1

There are 1 answers

0
PfhorSlayer On

I haven't tried this, but if you install your command using an OleMenuCommand, you can provide a "Changed" handler, which seems like it should get called whenever the text in the combo box changes. This may allow you to do what you want?