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. C#
  4. Is there a clean way to remove a control and it's associated event handlers?

Is there a clean way to remove a control and it's associated event handlers?

Scheduled Pinned Locked Moved C#
csharpquestion
6 Posts 4 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.
  • W Offline
    W Offline
    work_to_live
    wrote on last edited by
    #1

    I haven't found a clean way to remove a control (say a button) and any associated event handlers. What I do now is delete the control from the form, then find the event handlers in the code, and delete them. When I recompile, I get errors due to the deleted event handler code, and have to perform more surgery to clean them up. I'm new to dotNet, so be kind... There must be an easier way!

    F J I 3 Replies Last reply
    0
    • W work_to_live

      I haven't found a clean way to remove a control (say a button) and any associated event handlers. What I do now is delete the control from the form, then find the event handlers in the code, and delete them. When I recompile, I get errors due to the deleted event handler code, and have to perform more surgery to clean them up. I'm new to dotNet, so be kind... There must be an easier way!

      F Offline
      F Offline
      Frank Olorin Rizzi
      wrote on last edited by
      #2

      If you're talking at design time, then I have run into the same "problem"... removing a control from the design view removes the declaration and instanciation of that control from the IDE-generated code but not anything related like the even handlers.. ..I usually do that by hand. If you're talking at run time, I think that removing the control from the Controls collection of the form should do the trick (the handlers are still in the compiled code but they will not get called...) HTH, Olorin

      W 1 Reply Last reply
      0
      • W work_to_live

        I haven't found a clean way to remove a control (say a button) and any associated event handlers. What I do now is delete the control from the form, then find the event handlers in the code, and delete them. When I recompile, I get errors due to the deleted event handler code, and have to perform more surgery to clean them up. I'm new to dotNet, so be kind... There must be an easier way!

        J Offline
        J Offline
        James T Johnson
        wrote on last edited by
        #3

        Any handlers added by using the IDE will get removed when you delete the control. If you add handlers by hand then you'll have to remove them by hand as well. James "I despise the city and much prefer being where a traffic jam means a line-up at McDonald's" Me when telling a friend why I wouldn't want to live with him

        W 1 Reply Last reply
        0
        • W work_to_live

          I haven't found a clean way to remove a control (say a button) and any associated event handlers. What I do now is delete the control from the form, then find the event handlers in the code, and delete them. When I recompile, I get errors due to the deleted event handler code, and have to perform more surgery to clean them up. I'm new to dotNet, so be kind... There must be an easier way!

          I Offline
          I Offline
          Ista
          wrote on last edited by
          #4

          You can also create a macro to do that for you

          1 Reply Last reply
          0
          • F Frank Olorin Rizzi

            If you're talking at design time, then I have run into the same "problem"... removing a control from the design view removes the declaration and instanciation of that control from the IDE-generated code but not anything related like the even handlers.. ..I usually do that by hand. If you're talking at run time, I think that removing the control from the Controls collection of the form should do the trick (the handlers are still in the compiled code but they will not get called...) HTH, Olorin

            W Offline
            W Offline
            work_to_live
            wrote on last edited by
            #5

            Sorry, I was referencing design time. The functionality you reference is what I have seen. It just seems odd that you can't delete the event handlers in one fell swoop. Oh well. Thanks for your reply.

            1 Reply Last reply
            0
            • J James T Johnson

              Any handlers added by using the IDE will get removed when you delete the control. If you add handlers by hand then you'll have to remove them by hand as well. James "I despise the city and much prefer being where a traffic jam means a line-up at McDonald's" Me when telling a friend why I wouldn't want to live with him

              W Offline
              W Offline
              work_to_live
              wrote on last edited by
              #6

              What I see is the declaration and instantiation of a control (say a button) is removed when you delete the button, but not any associated event handlers, not even those created with the IDE.

              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