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