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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. get handlers from existing events in vb in an easy way

get handlers from existing events in vb in an easy way

Scheduled Pinned Locked Moved Visual Basic
questioncsharp
8 Posts 2 Posters 3 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.
  • C Offline
    C Offline
    christoph33
    wrote on last edited by
    #1

    for the existing MenuStrip-Item I want to create an appropriate ToolStrip with identical buttons. But how can I access the existing click-events? I cannot "addhandler mmi.click, adressof mi.click" and "adressof mi.onclick" also fails because it's protected. In c# I must add the handlers manually, so I know them, but in vb it's hidden... For Each Mi As ToolStripItem In FileMenu.DropDownItems If TypeOf (Mi) Is ToolStripMenuItem Then Dim mmi As New ToolStripButton mmi.Text = "" mmi.Image = Mi.Image mmi.ToolTipText = Mi.Text addhandler mmi.click, "get the handler of the mi.click-event" tsProgramm.Items.Add(mmi) ElseIf TypeOf (Mi) Is ToolStripSeparator Then Dim mmi As New ToolStripSeparator tsProgramm.Items.Add(mmi) Else End If Next Thanks in advance Chris

    M 1 Reply Last reply
    0
    • C christoph33

      for the existing MenuStrip-Item I want to create an appropriate ToolStrip with identical buttons. But how can I access the existing click-events? I cannot "addhandler mmi.click, adressof mi.click" and "adressof mi.onclick" also fails because it's protected. In c# I must add the handlers manually, so I know them, but in vb it's hidden... For Each Mi As ToolStripItem In FileMenu.DropDownItems If TypeOf (Mi) Is ToolStripMenuItem Then Dim mmi As New ToolStripButton mmi.Text = "" mmi.Image = Mi.Image mmi.ToolTipText = Mi.Text addhandler mmi.click, "get the handler of the mi.click-event" tsProgramm.Items.Add(mmi) ElseIf TypeOf (Mi) Is ToolStripSeparator Then Dim mmi As New ToolStripSeparator tsProgramm.Items.Add(mmi) Else End If Next Thanks in advance Chris

      M Offline
      M Offline
      MidwestLimey
      wrote on last edited by
      #2

      Where is this existing click handler coming from? If you haven't created and assigned one, it doesn't yet exist. If you have, then keep a reference to the delegate for later assignment. If you're refering to one used by the control internally, it maybe accessible via reflection - but that would be very poor design.


      I'm largely language agnostic


      After a while they all bug me :doh:


      C 1 Reply Last reply
      0
      • M MidwestLimey

        Where is this existing click handler coming from? If you haven't created and assigned one, it doesn't yet exist. If you have, then keep a reference to the delegate for later assignment. If you're refering to one used by the control internally, it maybe accessible via reflection - but that would be very poor design.


        I'm largely language agnostic


        After a while they all bug me :doh:


        C Offline
        C Offline
        christoph33
        wrote on last edited by
        #3

        MidwestLimey wrote:

        Where is this existing click handler coming from?

        I wrote it, every menuitem has a eg fileopen_click(sender, e) handles fileopen.click.

        MidwestLimey wrote:

        If you have, then keep a reference to the delegate for later assignment.

        Isn't there a get-the-address-of-the-onclick-event--function in the .net-jungle?

        MidwestLimey wrote:

        If you're refering to one used by the control internally, it maybe accessible via reflection - but that would be very poor design.

        Ugly, yes... Fastest way would be coding out every menuitem, but that's not very genious. Thanks for your answer Chris

        C 1 Reply Last reply
        0
        • C christoph33

          MidwestLimey wrote:

          Where is this existing click handler coming from?

          I wrote it, every menuitem has a eg fileopen_click(sender, e) handles fileopen.click.

          MidwestLimey wrote:

          If you have, then keep a reference to the delegate for later assignment.

          Isn't there a get-the-address-of-the-onclick-event--function in the .net-jungle?

          MidwestLimey wrote:

          If you're refering to one used by the control internally, it maybe accessible via reflection - but that would be very poor design.

          Ugly, yes... Fastest way would be coding out every menuitem, but that's not very genious. Thanks for your answer Chris

          C Offline
          C Offline
          christoph33
          wrote on last edited by
          #4

          I got a solution, but with that I have to declare all Menuitem_Click-Subs as Public and I have to set an InitFlag into them to supress invocation, because CallByName calls the event... For Each Mi As ToolStripItem In FileMenu.DropDownItems If TypeOf (Mi) Is ToolStripMenuItem Then Dim mmi As New ToolStripButton mmi.Text = "" mmi.Image = Mi.Image mmi.ToolTipText = Mi.Text Dim pArray() As Object = {Mi, New System.EventArgs} Dim delegateMn As System.EventHandler = CType(CallByName(Me, Mi.Name & "_Click", CallType.Method), System.EventHandler) AddHandler mmi.Click, delegateMn tsProgramm.Items.Add(mmi) ElseIf TypeOf (Mi) Is ToolStripSeparator Then Dim mmi As New ToolStripSeparator tsProgramm.Items.Add(mmi) Else End If Next

          M 1 Reply Last reply
          0
          • C christoph33

            I got a solution, but with that I have to declare all Menuitem_Click-Subs as Public and I have to set an InitFlag into them to supress invocation, because CallByName calls the event... For Each Mi As ToolStripItem In FileMenu.DropDownItems If TypeOf (Mi) Is ToolStripMenuItem Then Dim mmi As New ToolStripButton mmi.Text = "" mmi.Image = Mi.Image mmi.ToolTipText = Mi.Text Dim pArray() As Object = {Mi, New System.EventArgs} Dim delegateMn As System.EventHandler = CType(CallByName(Me, Mi.Name & "_Click", CallType.Method), System.EventHandler) AddHandler mmi.Click, delegateMn tsProgramm.Items.Add(mmi) ElseIf TypeOf (Mi) Is ToolStripSeparator Then Dim mmi As New ToolStripSeparator tsProgramm.Items.Add(mmi) Else End If Next

            M Offline
            M Offline
            MidwestLimey
            wrote on last edited by
            #5

            Why not declare the delegate ahead of time and then assign to each item's event?

            Dim myDelegate As EventHandler = AddressOf MenuItem_Click

            Where MenuItem_Click is a generic event handler for tool strip menu items.


            I'm largely language agnostic


            After a while they all bug me :doh:


            C 1 Reply Last reply
            0
            • M MidwestLimey

              Why not declare the delegate ahead of time and then assign to each item's event?

              Dim myDelegate As EventHandler = AddressOf MenuItem_Click

              Where MenuItem_Click is a generic event handler for tool strip menu items.


              I'm largely language agnostic


              After a while they all bug me :doh:


              C Offline
              C Offline
              christoph33
              wrote on last edited by
              #6

              Then I have to write 15 or more new delegates? Right? The aim was to add a toolstrip to the existing menustrip to act like a buddy. The new toolstrips get all properties from the existing menustrips and share the same events (there are more than 1 menustrip in the mdi). So I thought I could put some code-lines into a loop....

              M 1 Reply Last reply
              0
              • C christoph33

                Then I have to write 15 or more new delegates? Right? The aim was to add a toolstrip to the existing menustrip to act like a buddy. The new toolstrips get all properties from the existing menustrips and share the same events (there are more than 1 menustrip in the mdi). So I thought I could put some code-lines into a loop....

                M Offline
                M Offline
                MidwestLimey
                wrote on last edited by
                #7

                If I understand what you're trying to do (bear with me, I'm slow today!), then wouldn't it be easier to add a general event handler to the buddy strip buttons that calls PerformClick on the appropriate menu buttons? You could maintain context via either the Tag property or a dictionary.


                I'm largely language agnostic


                After a while they all bug me :doh:


                C 1 Reply Last reply
                0
                • M MidwestLimey

                  If I understand what you're trying to do (bear with me, I'm slow today!), then wouldn't it be easier to add a general event handler to the buddy strip buttons that calls PerformClick on the appropriate menu buttons? You could maintain context via either the Tag property or a dictionary.


                  I'm largely language agnostic


                  After a while they all bug me :doh:


                  C Offline
                  C Offline
                  christoph33
                  wrote on last edited by
                  #8

                  That would be a very good solution. I'll try it out. Thanks Chris

                  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