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
The sender is the BarManager. Use e.Item instead.