Respond to Multiple VSTO Context Menus in VB.Net

五迷三道 提交于 2019-12-13 08:47:39

问题


Trying to build a VSTO w/ VB.Net. I did ask, it has to be VB.Net.

Application Level ADDIN targeted at EXCEL

My challenge is trying to build a context menu that has an indeterminate number of children.

I can make the Menu, but I currently only get the onClick to fire for the last button.

I have researched this quite thoroughly and I can not find a VB example. There are plenty examples in VB of adding a simple context menu.

There are examples in C#, but you do something like this

 cb.Click += cbButton_Clicked;

I have yet to figure out how to do that in VB, hence the question. The entirety of the code is below. I can click on any of the first 5 buttons, and nothing happens. When I click on the last one, it fires a message box as expected.

I will have an indeterminate number of menu items, so I can not just add X # handlers.

Public Class ThisAddIn
 Dim WithEvents cb As CommandBarButton
    Dim TradeName As String = "MyMenu"
    Private Sub ThisAddIn_Startup() Handles Me.Startup

        ' AddHandler Application.WorkbookBeforeSave, AddressOf Application_WorkbookBeforeSave

        AddHandler Application.NewWorkbook, AddressOf ThisWorkbook_NewWorkbook

    End Sub


    Private Sub ThisWorkbook_NewWorkbook(wb As Microsoft.Office.Interop.Excel.Workbook)

        AddMenu2()
    End Sub



    Public Function GetCellContextMenu() As Office.CommandBar

        Return Application.CommandBars("Cell")
    End Function


    Public Sub AddMenu2()
        Dim Bar As Microsoft.Office.Core.CommandBar
        Dim NewControl As Microsoft.Office.Core.CommandBarControl

        Try

            Application.CommandBars("Cell").Controls(TradeName).Delete()
        Catch ex As Exception

        End Try


        Bar = Application.CommandBars("Cell")
        NewControl = Bar.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlPopup, Id:=1, Temporary:=True)

        With NewControl
            .Caption = TradeName
            .BeginGroup = True
            .TooltipText = TradeName & " Queries."
        End With

        Dim ag As New fvConnectionSuperAg
        Dim l As List(Of fvConnection) = ag.Items

        For Each conn As fvConnection In l

            cb = NewControl.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton)
            With cb
                .Caption = conn.Name.Value
                .FaceId = 218
                   ' HERE IS WHERE  I THINK I SHOULD CONNECT THE HANDLER 
            End With

Next

    End Sub

    Private Sub cb_Click(Ctrl As CommandBarButton, ByRef CancelDefault As Boolean) Handles cb.Click
        MsgBox(Ctrl.Caption, MsgBoxStyle.ApplicationModal, "Fast View")
    End Sub
End Class

回答1:


Never fails, posting on SO makes you clarify the question in your head , which often leads to an answer ....

I found the answer in this article that discusses adding event handlers programatically. Not related to VSTO really at all.

http://www.thescarms.com/dotnet/EventHandler.aspx

here is the excerpt from previous posting the difference s the ADDHANDLER call right after creating the button.

 cb = NewControl.Controls.Add (Microsoft.Office.Core.MsoControlType.msoControlButton)
            AddHandler cb.Click, AddressOf cb_Click
            With cb
                .Caption = conn.Name.Value
                .FaceId 
            End With


来源:https://stackoverflow.com/questions/46557192/respond-to-multiple-vsto-context-menus-in-vb-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!