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. simple radio button question

simple radio button question

Scheduled Pinned Locked Moved C#
questionhelp
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.
  • D Offline
    D Offline
    dino2094
    wrote on last edited by
    #1

    Another newbie needing help.... I need to 2 radio (or text) buttons so that both cannot be on at the same time. private void radioButton1_CheckedChanged(object sender, EventArgs e) { radioButton2.Checked = false; } private void radioButton2_CheckedChanged(object sender, EventArgs e) { radioButton1.Checked = false; } But it doesn't work when I have On radioButton1 OFF radioButton2 and when I click on radioButton2, radio button1 goes off but radiobutton2 also remains off. Do I have a circular event?

    L K E 3 Replies Last reply
    0
    • D dino2094

      Another newbie needing help.... I need to 2 radio (or text) buttons so that both cannot be on at the same time. private void radioButton1_CheckedChanged(object sender, EventArgs e) { radioButton2.Checked = false; } private void radioButton2_CheckedChanged(object sender, EventArgs e) { radioButton1.Checked = false; } But it doesn't work when I have On radioButton1 OFF radioButton2 and when I click on radioButton2, radio button1 goes off but radiobutton2 also remains off. Do I have a circular event?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, 1. if one rb is checked, and for some reason gets unchecked, then you also uncheck the other, hence both will be unchecked, which is NOT what you want. 2. no this is not circular, since a "changed" event only fires when the value CHANGES, not when a value gets assigned that equals the current value. 3. If you put your rb's in a GroupBox, then they behave like real rb's automatically (at most one checked). :)

      Luc Pattyn [My Articles]

      1 Reply Last reply
      0
      • D dino2094

        Another newbie needing help.... I need to 2 radio (or text) buttons so that both cannot be on at the same time. private void radioButton1_CheckedChanged(object sender, EventArgs e) { radioButton2.Checked = false; } private void radioButton2_CheckedChanged(object sender, EventArgs e) { radioButton1.Checked = false; } But it doesn't work when I have On radioButton1 OFF radioButton2 and when I click on radioButton2, radio button1 goes off but radiobutton2 also remains off. Do I have a circular event?

        K Offline
        K Offline
        Koocurrent
        wrote on last edited by
        #3

        The CheckedChanged event is raised when the radio button is checked or unchecked. The problem with your code above is that: Assuming 1 was previously checked when 2 is clicked, a checkedchanged for 2 is raised,which unchecked 1 this raises checkedchanged for 1 which also disables 2. At such both are disable (The scenario above assumes that the control are in different containers) You could use this code private void radioButton1_CheckedChanged(object sender, EventArgs e) { radioButton2.Checked = !radioButton1.Checked; } private void radioButton2_CheckedChanged(object sender, EventArgs e) { radioButton1.Checked = !radioButton2.Checked; }

        1 Reply Last reply
        0
        • D dino2094

          Another newbie needing help.... I need to 2 radio (or text) buttons so that both cannot be on at the same time. private void radioButton1_CheckedChanged(object sender, EventArgs e) { radioButton2.Checked = false; } private void radioButton2_CheckedChanged(object sender, EventArgs e) { radioButton1.Checked = false; } But it doesn't work when I have On radioButton1 OFF radioButton2 and when I click on radioButton2, radio button1 goes off but radiobutton2 also remains off. Do I have a circular event?

          E Offline
          E Offline
          engsrini
          wrote on last edited by
          #4

          the suggested solutions will work, even you can simplfy this by putting both the radio buttons in Group box and there is no need to write any code. group box itself will take care these logic

          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