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. C#
  4. how to add event over a control at runtime

how to add event over a control at runtime

Scheduled Pinned Locked Moved C#
csharpquestionhelptutorial
4 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.
  • S Offline
    S Offline
    shaz jazz
    wrote on last edited by
    #1

    i am using c#.net. i have added some controls(textbox and checkbox and labels) on a pannel when a button is cliked in form.now i want to add the event(CheckedChanged) on check box.normally its not difficult but i create the control dynamically so how can i add the event dyanmically? further more there is no fixed no of check boxes to be added so i cant make the event handlers prior to execution..

    May God help u solv ur problems as well,thx in advance!!

    J E S 3 Replies Last reply
    0
    • S shaz jazz

      i am using c#.net. i have added some controls(textbox and checkbox and labels) on a pannel when a button is cliked in form.now i want to add the event(CheckedChanged) on check box.normally its not difficult but i create the control dynamically so how can i add the event dyanmically? further more there is no fixed no of check boxes to be added so i cant make the event handlers prior to execution..

      May God help u solv ur problems as well,thx in advance!!

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      Firstly you can use the same event handler for all your dynamic checkboxes, you can always determine WHICH one was actually checked (or unchecked) using the sender parameter private void DynamicCheckboxes_CheckedChanged(object sender, EventArgs e) { CheckBox checkBox = (CheckBox)sender; Debug.Write("CheckBox: " + checkBox.Name + " checked:" + checkBox.Checked.ToString()); } Now when you dynamically create a checkbox in code you add an event handler thusly: CheckBox myCheckBox = new CheckBox(); myCheckBox.CheckedChanged += new EventHandler(DynamicCheckboxes_CheckedChanged_;

      --- How to get answers to your questions[^]

      1 Reply Last reply
      0
      • S shaz jazz

        i am using c#.net. i have added some controls(textbox and checkbox and labels) on a pannel when a button is cliked in form.now i want to add the event(CheckedChanged) on check box.normally its not difficult but i create the control dynamically so how can i add the event dyanmically? further more there is no fixed no of check boxes to be added so i cant make the event handlers prior to execution..

        May God help u solv ur problems as well,thx in advance!!

        E Offline
        E Offline
        ejuanpp
        wrote on last edited by
        #3

        Hi, you need to set an event handler to your control (most likely after having created it). Check the += operator applied to events. yourcontrol.CheckedChanged += twice and VS will complete the line and create a stub for your event handler>; Have a look at InitializeComponent() in your form. Regards.

        1 Reply Last reply
        0
        • S shaz jazz

          i am using c#.net. i have added some controls(textbox and checkbox and labels) on a pannel when a button is cliked in form.now i want to add the event(CheckedChanged) on check box.normally its not difficult but i create the control dynamically so how can i add the event dyanmically? further more there is no fixed no of check boxes to be added so i cant make the event handlers prior to execution..

          May God help u solv ur problems as well,thx in advance!!

          S Offline
          S Offline
          Sebastian Schneider
          wrote on last edited by
          #4

          In addition to what the previous posters said, you can always iterate through the controls-collection, find all the controls that are of type checkbox, and add an event-handler for them. Just give them a name so that you can find them again, if you need to.

          Cheers, Sebastian -- Contra vim mortem non est medicamen in hortem.

          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