0

In C# or else VB.Net, using a Visual Studio Package, how I could handle the event that is raised when the contextmenu of the current code window editor is opening and/or fully open?.

My intention is to arbitrary enable/disable the CommandBarButton buttons that are inside of my CommandBarPopup menu by evaluating a condition when the contextmenu is opening or fully open.

But I can't find any information about this, neither the event name or which class from the SDK is involved in this question, anything.


UPDATE

This is a code example following @Carlos Quintero indications, however, the BeforeQueryStatus event is never raised until I press the button once and the callback is done, why? how to fix it?.

As I said, I need to be able to control the button states (enabled/disabled) when the contextmenu of the code editor opens.

Protected Overrides Sub Initialize()

    Debug.WriteLine(String.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", Me.GetType().Name))
    MyBase.Initialize()

    ' Add our command handlers for menu (commands must exist in the .vsct file)
    Dim mcs As OleMenuCommandService = TryCast(GetService(GetType(IMenuCommandService)), OleMenuCommandService)

    If Not mcs Is Nothing Then

        ' Create the command for the menu item.
        Dim menuCommandID As New CommandID(GuidList.GuidSnippetToolCmdSet, CInt(PkgCmdIDList.cmdidMyCommand))
        Dim menuItem As New OleMenuCommand(New EventHandler(AddressOf MenuItemCallback), menuCommandID)
        AddHandler menuItem.BeforeQueryStatus, AddressOf OnBeforeQueryStatus

        mcs.AddCommand(menuItem)
    End If

End Sub

Private Sub MenuItemCallback(ByVal sender As Object, ByVal e As EventArgs)

    ' Show a Message Box to prove we were here
    Dim uiShell As IVsUIShell = TryCast(GetService(GetType(SVsUIShell)), IVsUIShell)
    Dim clsid As Guid = Guid.Empty
    Dim result As Integer
    Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(uiShell.ShowMessageBox(0, clsid, "Snippet Tool", String.Format(CultureInfo.CurrentCulture, "Inside {0}.MenuItemCallback()", Me.GetType().Name), String.Empty, 0, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST, OLEMSGICON.OLEMSGICON_INFO, 0, result))

End Sub

Private Sub OnBeforeQueryStatus(ByVal sender As Object, ByVal e As EventArgs)

    Dim myCommand As OleMenuCommand = TryCast(sender, OleMenuCommand)

    ' Alternate the command enable, just for testing.
    myCommand.Enabled = Not myCommand.Enabled

End Sub
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417

1 Answers1

2

There is no such event for context menus. When something happens (such as a context menu is shown, the selection is changed, the solution is closed, etc.), VS queries all commands about their status.

The way to do what you want is to use OleMenuCommand, BeforeQueryStatus event. In that event (that you don't know what caused it) you set the properties (enabled, visible, etc.) of the command depending on your conditions. See: How to: Create and Handle Commands in VSPackages (C#)

Carlos Quintero
  • 4,300
  • 1
  • 11
  • 18
  • I seen an answer of you in MSDN forums about a guy that asked more or less the same question as I, and you asked him: "For an Addin, or for a Pakacge?" befor explaining anything, I supposse you asked him that because that means there is a big difference between an addin and a package to query the command?, because I seen your article about BeforeQueryStatus event before asking this question, but i seen the example is for an AddIn, I've tried to implement it in my package without success. – ElektroStudios Jul 10 '15 at 12:40
  • We will continue the discussion in your "VS 2013 SDK: How to add a line separator in a CommandBarPopup menu?" thread – Carlos Quintero Jul 13 '15 at 10:55
  • I have much complications with this. the `BeforeQueryStatus` event is never raised until I press the button once and the callback finishes, so, If my button starts disabled with flag `DefaultDisabled` then that event will never raise because I need to press the button once as I said, the event does not raise even when I open the code editor window's context menu, never until I press the button once. Why? what I'm doing wrong?. Please see quetion update – ElektroStudios Jul 13 '15 at 19:32
  • Solved, I didn't remembered that I should manually initialize the commands with ``, otherwisse no matter how many times I open the xontextmenu, the buttons are created and shown anyways but... without an autoload does not fires the event. – ElektroStudios Jul 13 '15 at 20:08
  • 1
    Yes, the event is not fired until the package is loaded, either with autoload or executing a command. So, if your package absolutely needs to evaluate a condition to set the status of a command that cannot be set through command flags in the .vsct file, then you need autoload. – Carlos Quintero Jul 14 '15 at 15:06