Protected Shared mnuAbout As CommandBarControl Public Shared WithEvents mnuAboutHandler As CommandBarEvents 'EnvDTE.CommandBarEvents
I get the menu created, but the menu handler event never fires.
Private Shared Sub mnuAboutHandler_Click(ByVal CommandBarControl As Object, ByRef handled As Boolean, ByRef CancelDefault As Boolean) Handles mnuAboutHandler.Click
MsgBox(
"About what ") End SubCan anyone help

VisualStudio.CommandBars
Kumar Shetgar
Thanks
Les
CYH24937
You seem to be missing the part where you actually associate your subroutine with the click event.
I've done this a few times using C# code similar to the following:
private _DTE applicationObject;
private AddIn addInInstance;
private CommandBarButton btnHierIt = null;
public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
{
applicationObject = (_DTE)application;
addInInstance = (AddIn)addInInst;
if (btnHierIt == null)
{
try
{
Object objMissing = System.Reflection.Missing.Value;
CommandBar cmdBar = applicationObject.CommandBars["Tools"];
btnHierIt = (CommandBarButton)cmdBar.Controls.Add(MsoControlType.msoControlButton, objMissing, objMissing, objMissing, objMissing);
btnHierIt.Caption = "HierIt Test";
btnHierIt.FaceId = 1845;
btnHierIt.Click += new _CommandBarButtonEvents_ClickEventHandler(btnHierIt_Click);
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.Message);
}
}
}
private void btnHierIt_Click(CommandBarButton ctrl, ref bool cancelDefault)
{
// do something interesting
}
Sincerely,
Ed Dore [MSFT]
Jim Harrison IsaDewd
I have 3 menus with events and I am getting really weird results on menu events. All menu events are firing on any single menu click. Here is the relevant code.
Public Class Connect< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
Implements IDTExtensibility2
Public Shared oVS As DTE2
Dim _addInInstance As AddIn
Private cmdbar As Microsoft.VisualStudio.CommandBars.CommandBar
Private WithEvents mnuAbout As CommandBarButton
Private WithEvents mnuSetup As CommandBarButton
Private WithEvents mnuTest As CommandBarButton
Public Sub OnConnection(ByVal application As Object, ByVal connectMode As ext_ConnectMode, ByVal addInInst As Object, ByRef custom As Array) Implements IDTExtensibility2.OnConnection
Try
oVS = CType(application, DTE2)
_addInInstance = CType(addInInst, AddIn)
CreateMenus()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub CreateMenus()
Try
Dim nullParam As Object = Type.Missing
Dim cmdbar As CommandBar = CType(CType(oVS.CommandBars, CommandBars)("Code Window"), CommandBar)
Dim popup As CommandBarPopup = CType(cmdbar.Controls.Add(MsoControlType.msoControlPopup, nullParam, nullParam, 2, nullParam), CommandBarPopup)
popup.Caption = "Net Completer/VB"
Dim popup2 As CommandBarPopup = CType(popup.Controls.Add(MsoControlType.msoControlPopup, nullParam, nullParam, 1, nullParam), CommandBarPopup)
popup2.Caption = "Misc"
mnuAbout = CType(popup2.Controls.Add(MsoControlType.msoControlButton), CommandBarButton)
mnuAbout.Caption = "About"
mnuSetup = CType(popup2.Controls.Add(MsoControlType.msoControlButton), CommandBarButton)
mnuSetup.Caption = "Setup"
mnuTest = CType(popup.Controls.Add(MsoControlType.msoControlButton), CommandBarButton)
mnuTest.Caption = "Test"
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub
I placed the tests for ctrl.caption in the events to keep from doing anything if the event was fired falsely. Here is the output from the debug window:
mnuSetup_Click called from click on button with caption: About
mnuTest_Click called from click on button with caption: About
mnuAbout_Click called from click on button with caption: Setup
mnuTest_Click called from click on button with caption: Setup
mnuAbout_Click called from click on button with caption: Test
mnuSetup_Click called from click on button with caption: Test
Any ideas
thanks
Les
RobbyAnz
Private WithEvents cmdButtonHandler As CommandBarEventsbut when I tried to execute the following code, I get an ArgumentException, Value does not fall within the expected range, on the last line trying to sink the handler.
cmdbar = oVS.CommandBars.item(
"MenuBar")cmdBarPopup = cmdbar.Controls.Item(
"File")cmdbutton = cmdBarPopup.Controls.Add(Microsoft.VisualStudio.CommandBars.MsoControlType.msoControlButton)
cmdbutton.Caption =
"Test"cmdButtonHandler =
CType(oVS.Events.CommandBarEvents(cmdButtonHandler), CommandBarEvents)I am not sure how to declare and sink the cmdButton handler
----------------------------------------------------------------
I was using the following code and it gets the menu and submenus displayed, but the click event never fires
cmdBarCW =
CType(oVB.CommandBars("Code Window"), CommandBar) ' create popup menuspopCSPlusMenu = cmdBarCW.Controls.Add(MsoControlType.msoControlPopup, _
System.Reflection.Missing.Value, _
System.Reflection.Missing.Value, _
Before:=1, Temporary:=
True)popCSPlusMenu.Caption =
"&CSharpCompleterPlus"' create the menuitem
CMenus.mnuShowBlock = AddOfficeMenuItem(popCSPlusMenu,
"Show Block")CMenus.mnuShowBlockHandler =
CType(oVB.Events.CommandBarEvents(mnuShowBlock), EnvDTE.CommandBarEvents)' the metod that adds the menu is
Public Shared Function AddOfficeMenuItem(ByVal _
Menu
As CommandBarControl, _ ByVal Caption As String, _ Optional ByVal pos As Byte = 0, _ Optional ByVal sep As Boolean = False, _ Optional ByVal Bitmap As Object = Nothing) _ As CommandBarControl Dim menuItem As CommandBarControl Try If Not (Bitmap Is Nothing) ThenSystem.Windows.Forms.Clipboard.SetDataObject(Bitmap)
End If ' Add menu item to VB menu: If pos = 0 Then pos = CType(CType(Menu.Controls.Count, Integer) + 1, Byte)menuItem = _
Menu.Controls.Add(Type:=MsoControlType.msoControlButton, _
Before:=pos, _
Temporary:=
True) ' Set properties of menu item:menuItem.Caption = Caption
If sep Then menuItem.BeginGroup = True If Not (Bitmap Is Nothing) ThenmenuItem.Style = MsoButtonStyle.msoButtonIconAndCaption
'Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaptionmenuItem.PasteFace()
End If Return menuItem Catch e As System.ExceptionStructuredErrorHandler(e)
Return Nothing End Try End FunctionThanks
Les
pmolstad
I must be dense, but here is the simple code and the menu gets displayed but the event never fires. It can't be as hard as it seems....
Dim cmdbutton As Microsoft.VisualStudio.CommandBars.CommandBarButton Private cmdbar As Microsoft.VisualStudio.CommandBars.CommandBar Dim cmdBarPopup As Microsoft.VisualStudio.CommandBars.CommandBarPopup Public WithEvents cmdBarEvents As CommandBarEvents Public Sub OnConnection(ByVal application As Object, ByVal connectMode As ext_ConnectMode, ByVal addInInst As Object, ByRef custom As Array) Implements IDTExtensibility2.OnConnection Try
oVS =
CType(application, DTE2)_addInInstance =
CType(addInInst, AddIn)oVS =
CType(application, DTE2)cmdbar = oVS.CommandBars.item(
"MenuBar")cmdBarPopup = cmdbar.Controls.Item(
"File")cmdbutton = cmdBarPopup.Controls.Add(Microsoft.VisualStudio.CommandBars.MsoControlType.msoControlButton)
cmdbutton.Caption =
"Test"cmdBarEvents = oVS.Events.CommandBarEvents(cmdbutton)
Catch ex As ExceptionMsgBox(ex.ToString)
End Try End SubPrivate Sub cmdBarEvents_Click(ByVal CommandBarControl As Object, ByRef Handled As Boolean, ByRef CancelDefault As Boolean) Handles cmdBarEvents.Click
MsgBox(
"test") End SubGeorge Longmire
I did get it to work this time, but only after changing the signature of the click event to
ByVal
Ctrl As Microsoft.VisualStudio.CommandBars.CommandBarButton, ByRef CancelDefault As SByteThe event that you sent was not compatible with CancelDefault as Boolean
Thanks very much for your help.
Les
Les@KnowDotNet.com
Vasily Sokolov
Unless I'm missing something, I think that the sink is done by the
cmdBarEvents = oVS.Events.CommandBarEvents(cmdbutton)
seeing that cmdBarEvents was declared as
Public WithEvents cmdBarEvents As CommandBarEvents
Am I still missing something
Thanks
Les
Frank I.
Hi Les,
Do I ever need to brush up on my VB .Net. Been a long time since I've done any VB work. Sorry about that. I did get this working in a prototype addin written with VB .Net 2005, though admittedly it's a bit different than your implementation.
Mine looks more like the following:
Dim _applicationObject As DTE2
Dim _addInInstance As AddIn
Private WithEvents _myButton As CommandBarButton
Private Sub _myButton_click(ByVal ctrl As CommandBarButton, ByRef cancelDefault As Boolean) Handles _myButton.Click
MessageBox.Show("Hello from My Button")
End Sub
Public Sub OnConnection(ByVal application As Object, ByVal connectMode As ext_ConnectMode, ByVal addInInst As Object, ByRef custom As Array) Implements IDTExtensibility2.OnConnection
_applicationObject = CType(application, DTE2)
_addInInstance = CType(addInInst, AddIn)
Dim commandBars As CommandBars = CType(_applicationObject.CommandBars, CommandBars)
Dim menuBarCommandBar As CommandBar = commandBars.Item("MenuBar")
Dim toolsControl As CommandBarControl = menuBarCommandBar.Controls.Item("Tools")
Dim toolsPopup As CommandBarPopup = CType(toolsControl, CommandBarPopup)
_myButton = CType(toolsPopup.Controls.Add(MsoControlType.msoControlButton), CommandBarButton)
_myButton.Caption = "My Button"
_myButton.Visible = True
End Sub
Hopefully, that'll do the trick for you.
Sincerely,
Ed Dore [MSFT]
Kato12651
Looks like I missed out the last line of the code snippet
CmdBarEvents = DTE.Events.CommandBarEvents(cmdbutton)
The argument is the Control and not the handler. Since you are passing the handler you get an argument exception.
Thanks,
Chetan
Rizon406
Hi Les,
What method are you using to create the menu The CommandBarEvents work only with the old style of creating commandbars. With Visual Studio 2005 we have deprecated this method. What I mean by using the old style is creating a menu command using code similar to the following:
Dim cmdbutton As Microsoft.VisualStudio.CommandBars.CommandBarButton Dim cmdbar As Microsoft.VisualStudio.CommandBars.CommandBar Dim cmdBarPopup As Microsoft.VisualStudio.CommandBars.CommandBarPopup
cmdbar = DTE.CommandBars.item(
"MenuBar")cmdBarPopup = cmdbar.Controls.Item(
"File")cmdbutton = cmdBarPopup.Controls.Add(Microsoft.VisualStudio.CommandBars.MsoControlType.msoControlButton)
cmdbutton.Caption =
"Test"Thanks,
chetan
BennyAW
I wrote this macro to test events and they’re working correctly in VS 2005 50727.42. There was a late breaking bug in this area which would cause the behavior you’re describing, but it should have been fixed in RTM. < xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
Private WithEvents m_btn1 As CommandBarButton
Private WithEvents m_btn2 As CommandBarButton
Private Sub OnButton1Clicked(ByVal Ctrl As CommandBarButton, ByRef CancelDefault As Boolean) Handles m_btn1.Click
MsgBox("Click from control " & Ctrl.Caption & " through handler OnButton1Clicked")
End Sub
Private Sub OnButton2Clicked(ByVal Ctrl As CommandBarButton, ByRef CancelDefault As Boolean) Handles m_btn2.Click
MsgBox("Click from control " & Ctrl.Caption & " through handler OnButton2Clicked")
End Sub
Public Sub TestClickEvents()
Dim toolsMenu As CommandBar = DTE.CommandBars.Item("Tools")
m_btn1 = toolsMenu.Controls.Add(MsoControlType.msoControlButton)
m_btn1.Caption = "Button1"
m_btn2 = toolsMenu.Controls.Add(MsoControlType.msoControlButton)
m_btn2.Caption = "Button2"
End Sub
Does this help
bakerbud9
Very curious. I copy/pasted that code right from a newly created addin where the click event was firing. If I get a chance tomorrow, I'll try and figure out what the deal is with why that CancelDefault had to be declared as an SByte. You didn't by chance have an earlier Whidbey drop on that box at one point
Sincerely,
Ed Dore [MSFT]