Add handler during runtime "problem"
-
Question: how do I get this to work with Option strict On ? I'll add a bunch of ToolstripMenuitems(dropdowns) at runtime and need to point them to ONE single click event handler It "all" works with Option strict turned off :confused:.. but the compiler dosn't like it with Option strict turned ON If I change in the event handler sub ByVal sender As ToolStripMenuItem to ByVal sender As Object The compiler complains inside the sub when trying to read the [sender.Text] value ... Whats missing ? I know it dosn't like the narrowing - but I don't know how to fix it ....
Dim test As New ToolStripMenuItem
AddHandler test.Click, AddressOf IO_selected ' ** handler to be added **
InputsToolStripMenuItem.DropDownItems.Add(test)Private Sub IO_selected(ByVal sender As ToolStripMenuItem, ByVal e As System.EventArgs)
IODataView.CurrentCell.Value = sender.Text
End SubThanks Georg
-
Question: how do I get this to work with Option strict On ? I'll add a bunch of ToolstripMenuitems(dropdowns) at runtime and need to point them to ONE single click event handler It "all" works with Option strict turned off :confused:.. but the compiler dosn't like it with Option strict turned ON If I change in the event handler sub ByVal sender As ToolStripMenuItem to ByVal sender As Object The compiler complains inside the sub when trying to read the [sender.Text] value ... Whats missing ? I know it dosn't like the narrowing - but I don't know how to fix it ....
Dim test As New ToolStripMenuItem
AddHandler test.Click, AddressOf IO_selected ' ** handler to be added **
InputsToolStripMenuItem.DropDownItems.Add(test)Private Sub IO_selected(ByVal sender As ToolStripMenuItem, ByVal e As System.EventArgs)
IODataView.CurrentCell.Value = sender.Text
End SubThanks Georg
Georg Kohler wrote:
If I change in the event handler sub ByVal sender As ToolStripMenuItem to ByVal sender As Object
That's because when the event is fired, it's epecting the handlers signature to be an exact match to what it expects.
sender
MUST be declared asObject
.Georg Kohler wrote:
The compiler complains inside the sub when trying to read the [sender.Text] value ...
Obviously, the type
Object
doesn't have a Text property. So, you have to check for and cast thesender
to the type you want. Get used to this, it's pretty common to do this:Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sentMenuItem As ToolStripMenuItem
If TypeOf sender Is ToolStopMenuItem Then
sentMenuItem = DirectCast(sender, ToolStripMenuItem)
End IfIODataView.CurrentCell.Value = sentMenuItem.Text
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
Georg Kohler wrote:
If I change in the event handler sub ByVal sender As ToolStripMenuItem to ByVal sender As Object
That's because when the event is fired, it's epecting the handlers signature to be an exact match to what it expects.
sender
MUST be declared asObject
.Georg Kohler wrote:
The compiler complains inside the sub when trying to read the [sender.Text] value ...
Obviously, the type
Object
doesn't have a Text property. So, you have to check for and cast thesender
to the type you want. Get used to this, it's pretty common to do this:Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sentMenuItem As ToolStripMenuItem
If TypeOf sender Is ToolStopMenuItem Then
sentMenuItem = DirectCast(sender, ToolStripMenuItem)
End IfIODataView.CurrentCell.Value = sentMenuItem.Text
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008Thanks - that makes sense ;)