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. Selected Checkbox...

Selected Checkbox...

Scheduled Pinned Locked Moved C#
question
4 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
    gonad
    wrote on last edited by
    #1

    Hi. i have a few checkboxes on my form and i want to show a simple messagebox when one of those checkboxes are checked. how can i do this dynamically? for instance, instead of writing an if statement per checkbox, like this: if(checkbox1.checked = true) { MessageBox.Show("checked"); } write it dynamically, like this: (the is the name of the selected checkbox) if(.checked = true) { MessageBox.Show("checked"); } i know this is pretty easy for all you guru friends out there. :) Thanks, .gonad.

    T 1 Reply Last reply
    0
    • G gonad

      Hi. i have a few checkboxes on my form and i want to show a simple messagebox when one of those checkboxes are checked. how can i do this dynamically? for instance, instead of writing an if statement per checkbox, like this: if(checkbox1.checked = true) { MessageBox.Show("checked"); } write it dynamically, like this: (the is the name of the selected checkbox) if(.checked = true) { MessageBox.Show("checked"); } i know this is pretty easy for all you guru friends out there. :) Thanks, .gonad.

      T Offline
      T Offline
      Tomas Petricek
      wrote on last edited by
      #2

      I'm not sure if I understand you, but you can maybe use first parameter of your event handler method (sender). You have to set same function as event handler for all checkboxes.

      private void checkBox1\_CheckedChanged(object sender, System.EventArgs e)
      {
        CheckBox checked=(CheckBox)sender;
        // checked is checkbox that was just checked/unchecked
      
      }
      

      i'm only pointer to myself

      G 1 Reply Last reply
      0
      • T Tomas Petricek

        I'm not sure if I understand you, but you can maybe use first parameter of your event handler method (sender). You have to set same function as event handler for all checkboxes.

        private void checkBox1\_CheckedChanged(object sender, System.EventArgs e)
        {
          CheckBox checked=(CheckBox)sender;
          // checked is checkbox that was just checked/unchecked
        
        }
        

        i'm only pointer to myself

        G Offline
        G Offline
        gonad
        wrote on last edited by
        #3

        Hi. Thanks for your help. this worked exactly as you said, but i do have a queston. Can you explain what the (CheckBox)sender is specifying? also, can i use this for a textbox, or other controls, as well? for example, TextBox tb = (TextBox)sender; I'm new at this stuff, can't you tell? Thanks again. .gonad.

        T 1 Reply Last reply
        0
        • G gonad

          Hi. Thanks for your help. this worked exactly as you said, but i do have a queston. Can you explain what the (CheckBox)sender is specifying? also, can i use this for a textbox, or other controls, as well? for example, TextBox tb = (TextBox)sender; I'm new at this stuff, can't you tell? Thanks again. .gonad.

          T Offline
          T Offline
          Tomas Petricek
          wrote on last edited by
          #4

          First parameter (sender) of event handler is affected object. it is passed as object so you need to cast it to correct type (Button / CheckBox etc..). So when you have event handler for Button, sender is button user clicked on. If you have event handler for TextBox, sender is this TextBox. i'm only pointer to myself

          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