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. Multiple Radio buttons In A Form

Multiple Radio buttons In A Form

Scheduled Pinned Locked Moved C#
question
8 Posts 6 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.
  • R Offline
    R Offline
    Ravindra Bisen
    wrote on last edited by
    #1

    I have 50 radio buttons in a form. can i use a loop to check which radio button is checked ? :zzz:

    L N L Y 4 Replies Last reply
    0
    • R Ravindra Bisen

      I have 50 radio buttons in a form. can i use a loop to check which radio button is checked ? :zzz:

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Yup. You'd typical loop through the Control-collection of the form, checking if the Control **is** a radioButton, and if it is, downcast it to a RadioButton and read the value. --edit-- Forgot to close one of the <code> tags

      I are troll :)

      R 1 Reply Last reply
      0
      • R Ravindra Bisen

        I have 50 radio buttons in a form. can i use a loop to check which radio button is checked ? :zzz:

        N Offline
        N Offline
        Nagy Vilmos
        wrote on last edited by
        #3

        Ravindra Bisen wrote:

        50 radio buttons in a form

        Why? If you've got this many options, maybe a dropdown combo or a list would be a better idea? With either if these finding the selected item is easy. Alternatively if you absolutly must have all the radio buttons, on the CheckedChange event catch the pressed button as it will be the object sender.


        Panic, Chaos, Destruction. My work here is done.

        1 Reply Last reply
        0
        • R Ravindra Bisen

          I have 50 radio buttons in a form. can i use a loop to check which radio button is checked ? :zzz:

          L Offline
          L Offline
          Luc 648011
          wrote on last edited by
          #4

          The alternative is to have a class member RadioButton LastChecked and to give all RadioButtons the same CheckedChanged handler, which simply updates LastChecked=(RadioButton)sender; for the last one whose Checked property became true. :)

          0 1 Reply Last reply
          0
          • L Luc 648011

            The alternative is to have a class member RadioButton LastChecked and to give all RadioButtons the same CheckedChanged handler, which simply updates LastChecked=(RadioButton)sender; for the last one whose Checked property became true. :)

            0 Offline
            0 Offline
            0x3c0
            wrote on last edited by
            #5

            It may be cleaner to use LastChecked = sender as RadioButton instead, then just check if LastChecked is null. That way, you don't have to go through exception handlers if you get an InvalidCastException

            1 Reply Last reply
            0
            • L Lost User

              Yup. You'd typical loop through the Control-collection of the form, checking if the Control **is** a radioButton, and if it is, downcast it to a RadioButton and read the value. --edit-- Forgot to close one of the <code> tags

              I are troll :)

              R Offline
              R Offline
              Ravindra Bisen
              wrote on last edited by
              #6

              hi i am using like this.. opt_no=1;score=0; foreach (Control rd in this.Controls) { string rdo= "radioButton"+opt_no"; if (rd.Name == rdo && rd is RadioButton) { // my code is here rd.Checked=true; // the line gives an error } opt_no= opt_no+1; } it takes only that control which is designed into form. but i designed all radio buttons into flowLayoutPanel. i have 100 question and each Que. has four option(means four Radio button which is created in a panel).i am checking score. i am taking question no and ans from a table.

              L 1 Reply Last reply
              0
              • R Ravindra Bisen

                I have 50 radio buttons in a form. can i use a loop to check which radio button is checked ? :zzz:

                Y Offline
                Y Offline
                Yusuf
                wrote on last edited by
                #7

                as williamnw pointed out, try to chnage you design. 50 radio buttons is way too much. It can be replaced with drop down or list control.

                Yusuf Oh didn't you notice, analogous to square roots, they recently introduced rectangular, circular, and diamond roots to determine the size of the corresponding shapes when given the area. Luc Pattyn[^]

                1 Reply Last reply
                0
                • R Ravindra Bisen

                  hi i am using like this.. opt_no=1;score=0; foreach (Control rd in this.Controls) { string rdo= "radioButton"+opt_no"; if (rd.Name == rdo && rd is RadioButton) { // my code is here rd.Checked=true; // the line gives an error } opt_no= opt_no+1; } it takes only that control which is designed into form. but i designed all radio buttons into flowLayoutPanel. i have 100 question and each Que. has four option(means four Radio button which is created in a panel).i am checking score. i am taking question no and ans from a table.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Ravindra Bisen wrote:

                  each Que. has four option(means four Radio button which is created in a panel)

                  Sounds like a user-control to me :)

                  I are troll :)

                  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