Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Add handler during runtime "problem"

Add handler during runtime "problem"

Scheduled Pinned Locked Moved Visual Basic
questionhelptutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Georg Kohler
    wrote on last edited by
    #1

    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 Sub

    Thanks Georg

    D 1 Reply Last reply
    0
    • G Georg Kohler

      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 Sub

      Thanks Georg

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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 as Object.

      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 the sender 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 If

          IODataView.CurrentCell.Value = sentMenuItem.Text
      

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007, 2008

      G 1 Reply Last reply
      0
      • D Dave Kreskowiak

        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 as Object.

        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 the sender 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 If

            IODataView.CurrentCell.Value = sentMenuItem.Text
        

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007, 2008

        G Offline
        G Offline
        Georg Kohler
        wrote on last edited by
        #3

        Thanks - that makes sense ;)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups