首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Outlook VisualStudio - Button Eventhandler

Outlook VisualStudio - Button Eventhandler
EN

Stack Overflow用户
提问于 2020-05-05 19:19:00
回答 1查看 24关注 0票数 0

自从几天以来,我一直在寻找解决方案,但我总是能得到解决问题的代码。我想通过VisualStudio在Outlook中创建几个按钮。这些按钮应该执行相同的子。但是当我用显示的代码创建按钮时,只有最后一个创建的按钮处理单击事件。

我使用的是VisualStudio (15.0)和Outlook (16.0,32位)

非常感谢你的帮助

霍格

代码语言:javascript
复制
Public Class ThisAddIn
    Dim ButtonControl As Office.CommandBarButton
    Dim menuBar As Office.CommandBar
    Dim newMenuBar As Office.CommandBarPopup

    Private Sub ThisAddIn_Startup() Handles Me.Startup
        Dim i As Integer

        menuBar = Me.Application.ActiveExplorer().CommandBars.ActiveMenuBar
        newMenuBar = menuBar.Controls.Add(Office.MsoControlType.msoControlPopup, Temporary:=True)
        If newMenuBar IsNot Nothing Then
            newMenuBar.Caption = "Mailverschiebung"

            For i = 0 To 3
                ButtonControl = newMenuBar.Controls.Add
                ButtonControl.Caption = "zeichen" & i
                ButtonControl.Tag = "zeichen" & i
                AddHandler ButtonControl.Click, AddressOf ButtonControl_Click

            Next
        End If
    End Sub

    Sub ButtonControl_Click()
        MsgBox("Läuft")
    End Sub

    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown

    End Sub

End Class
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-05 19:53:40

如果希望为所有按钮处理事件,则必须保持对象引用处于活动状态。因此,基本上,您需要在全局范围内定义一个列表或按钮数组。

代码语言:javascript
复制
Public Class ThisAddIn
    Dim ButtonControl As Office.CommandBarButton
    Dim ButtonControls As List(Of Office.CommandBarButton) = new List(Of Office.CommandBarButton)()
    Dim menuBar As Office.CommandBar
    Dim newMenuBar As Office.CommandBarPopup

    Private Sub ThisAddIn_Startup() Handles Me.Startup
        Dim i As Integer

        menuBar = Me.Application.ActiveExplorer().CommandBars.ActiveMenuBar
        newMenuBar = menuBar.Controls.Add(Office.MsoControlType.msoControlPopup, Temporary:=True)
        If newMenuBar IsNot Nothing Then
            newMenuBar.Caption = "Mailverschiebung"

            For i = 0 To 3
                ButtonControl = newMenuBar.Controls.Add
                ButtonControl.Caption = "zeichen" & i
                ButtonControl.Tag = "zeichen" & i
                AddHandler ButtonControl.Click, AddressOf ButtonControl_Click
                ButtonControls.Add(ButtonControl)
            Next
        End If
    End Sub

    Sub ButtonControl_Click()
        MsgBox("Läuft")
    End Sub

    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown

    End Sub

End Class

请注意,不推荐使用命令栏,不应使用命令条自定义Outlook。相反,您应该使用Fluent UI (也称为Ribbon )。在以下系列文章中阅读有关新UI的更多信息:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61621254

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档