Groups | Blog | Home
all groups > visual studio .net general > august 2006 >

visual studio .net general : VS Extensibility: Commandbar event for Popup or perhaps BeforeRightClick?


james.kolpack NO[at]SPAM gmail.com
8/25/2006 6:42:39 AM
I've got a control that I've added to the Commandbar for the code
window, and I'm trying to set the enabled state based on the selection
(or active point) for the current ActiveDocument. If the currently
selection is valid by some parameters, the control's enabled state will
be true, otherwise it will be false.

What I really want to do is have an event to fire before the commandbar
is shown, so that I can check where the selection is, see if it is
valid, and enable/disable the control before the commandbar is
presented to the user. I believe this same kind of functionality
occurs in addins like GhostDoc and Resharper - modifying enabled states
based on context.

Here is the code I'm working with now:

public void OnConnection(object application, ext_ConnectMode
connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;

// Get the CommandBar for the window in which you want to add the
menuitem. I am going to add it to the code window.
CommandBar commandBar = (_applicationObject.CommandBars as
CommandBars)["Code Window"];

// create a control
CommandBarControl gotoDeclarationControl = commandBar.Controls.Add(
MsoControlType.msoControlButton,
System.Reflection.Missing.Value, System.Reflection.Missing.Value, 1,
true);
gotoDeclarationControl.Caption = "Go to Declaration";

// add click handler for control
mGotoDeclarationItemHandler =
_applicationObject.Events.get_CommandBarEvents(gotoDeclarationControl)
as CommandBarEvents;
mGotoDeclarationItemHandler.Click += new
_dispCommandBarControlEvents_ClickEventHandler(mGotoDeclarationItemHandler_Click);
}


Anyone have any advice and/or a pointer to some relevant documentation?
Carlos J. Quintero [VB MVP]
8/28/2006 12:00:00 AM
There is no right-click event or similar, with good reason because controls
should be created from commands, and commands are what must be enabled or
disabled. You need to change your code to use a DTE command and use
Command.AddControl and the IDTCommandTarget.QueryStatus method to set the
state of your command. VS will call it when needed (before showing the
popup, for example). See my article:

HOWTO: Adding buttons, commandbars and toolbars to Visual Studio .NET from
an add-in.

at:

http://www.mztools.com/resources_vsnet_addins.htm


--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com


<james.kolpack@gmail.com> escribió en el mensaje
news:1156513359.109410.57600@m73g2000cwd.googlegroups.com...
[quoted text, click to view]

AddThis Social Bookmark Button