传递未实例化的发送者的事件处理程序

发布于 2024-10-03 15:51:35 字数 1201 浏览 1 评论 0原文

我正在动态创建按钮并为它们创建事件处理程序。当代码运行时,一切工作正常,只要成功地动态填充菜单即可。当我单击按钮时,事件会触发,但 sender=Nothing 且对象未设置为实例异常出现。有什么想法吗?一定是缺少了什么。

Imports DevExpress.XtraBars.Ribbon

Public Class Form1

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim menu As New RibbonControl
        Dim aPage As New RibbonPage("Nicks Page")
        'groups'
        Dim aGroup1 As New RibbonPageGroup("1st Group")

        'ADD BUTTONS TO RIBBON GROUP HERE'
        Dim i As New DevExpress.XtraBars.BarButtonItem()
        i.Caption = "Nicks Button"
        AddHandler i.ItemClick, AddressOf y
        aGroup1.ItemLinks.Add(i)

        Dim i2 As New DevExpress.XtraBars.BarButtonItem()
        i2.Caption = "Nicks Other Button"
        AddHandler i2.ItemClick, AddressOf y
        aGroup1.ItemLinks.Add(i2)

        aPage.Groups.Add(aGroup1)
        menu.Pages.Add(aPage)
        Me.Controls.Add(menu)
    End Sub

    Private Sub y(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs)
        'EXCEPTION SENDER=NOTHING MSGBOX FAILS'
        MsgBox(CType(sender, DevExpress.XtraBars.BarButtonItem).Caption)
    End Sub
End Class

I am creating buttons on the fly and creating event handlers for them. When the code runs everything works fine as far as populating the menu on the fly successfully. When I click a button the event fires, but sender=Nothing and the Object is not set to instance exception comes up. Any Ideas? Must be missing something.

Imports DevExpress.XtraBars.Ribbon

Public Class Form1

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim menu As New RibbonControl
        Dim aPage As New RibbonPage("Nicks Page")
        'groups'
        Dim aGroup1 As New RibbonPageGroup("1st Group")

        'ADD BUTTONS TO RIBBON GROUP HERE'
        Dim i As New DevExpress.XtraBars.BarButtonItem()
        i.Caption = "Nicks Button"
        AddHandler i.ItemClick, AddressOf y
        aGroup1.ItemLinks.Add(i)

        Dim i2 As New DevExpress.XtraBars.BarButtonItem()
        i2.Caption = "Nicks Other Button"
        AddHandler i2.ItemClick, AddressOf y
        aGroup1.ItemLinks.Add(i2)

        aPage.Groups.Add(aGroup1)
        menu.Pages.Add(aPage)
        Me.Controls.Add(menu)
    End Sub

    Private Sub y(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs)
        'EXCEPTION SENDER=NOTHING MSGBOX FAILS'
        MsgBox(CType(sender, DevExpress.XtraBars.BarButtonItem).Caption)
    End Sub
End Class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿嘿嘿 2024-10-10 15:51:35

发送者是BarManager。请改用 e.Item。

The sender is the BarManager. Use e.Item instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文