响应 VB.Net 中的多个 VSTO 上下文菜单 [英] Respond to Multiple VSTO Context Menus in VB.Net

查看:32
本文介绍了响应 VB.Net 中的多个 VSTO 上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用 VB.Net 构建 VSTO.我确实问过,它必须是 VB.Net.

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

针对 EXCEL 的应用级 ADDIN

Application Level ADDIN targeted at EXCEL

我的挑战是尝试构建一个包含不确定子项数量的上下文菜单.

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

我可以制作菜单,但我目前只能让 onClick 触发最后一个按钮.

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

我对此进行了非常彻底的研究,但找不到 VB 示例.在 VB 中有很多添加简单上下文菜单的示例.

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.

C# 中有一些例子,但你做这样的事情

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

 cb.Click += cbButton_Clicked;

我还没有弄清楚如何在 VB 中做到这一点,因此这个问题.整个代码如下.我可以单击前 5 个按钮中的任何一个,但没有任何反应.当我点击最后一个时,它会按预期触发一个消息框.

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.

我将有不确定数量的菜单项,所以我不能只添加 X # 处理程序.

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

下一步

    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

推荐答案

永远不会失败,在 SO 上发帖可以让您澄清脑海中的问题,这通常会导致答案....

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

我在讨论以编程方式添加事件处理程序的这篇文章中找到了答案.与 VSTO 完全没有关系.

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

这里是上一篇文章的摘录区别在于创建按钮后立即调用 ADDHANDLER.

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

这篇关于响应 VB.Net 中的多个 VSTO 上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆