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. How to check a groupbox for checked radiobuttons dynamicaly

How to check a groupbox for checked radiobuttons dynamicaly

Scheduled Pinned Locked Moved C#
tutorialquestion
10 Posts 3 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
    gwithey
    wrote on last edited by
    #1

    I have come across something and wondered if it can be done.

    private void Shape_CheckedChanged(object sender, EventArgs e)
    {
    // Check and set the draw state that has been selected
    if (rbDraw.Checked)
    {
    if (rbSelectShape.Checked)
    {
    imageViewer1.ShapeSelected = ImageViewer.Shape.Select;
    }
    if (rbDrawRect.Checked)
    {
    imageViewer1.ShapeSelected = ImageViewer.Shape.Rectangle;
    }
    if (rbDrawCircle.Checked)
    {
    imageViewer1.ShapeSelected = ImageViewer.Shape.Circle;
    }
    if (rbDrawTriangle.Checked)
    {
    imageViewer1.ShapeSelected = ImageViewer.Shape.Triangle;
    }
    if (rbLine.Checked)
    {
    imageViewer1.ShapeSelected = ImageViewer.Shape.Line;
    }
    if (rbText.Checked)
    {
    imageViewer1.ShapeSelected = ImageViewer.Shape.Text;
    }
    }
    Console.WriteLine(imageViewer1.ShapeSelected.ToString());
    }

    Above is the code i used to check each radio button however i tried tidying it up with the bellow code and couldn't think of a way to set the enum depending on checked radio button.

    foreach (RadioButton rb in gbShape)
    {
    if (rb.Checked == true)
    {
    imageViewer1.ShapeSelected = ImageViewer.Shape.Circle; // How can make dynamic?
    }
    }

    Any ideas Thanx in advance

    D 1 Reply Last reply
    0
    • G gwithey

      I have come across something and wondered if it can be done.

      private void Shape_CheckedChanged(object sender, EventArgs e)
      {
      // Check and set the draw state that has been selected
      if (rbDraw.Checked)
      {
      if (rbSelectShape.Checked)
      {
      imageViewer1.ShapeSelected = ImageViewer.Shape.Select;
      }
      if (rbDrawRect.Checked)
      {
      imageViewer1.ShapeSelected = ImageViewer.Shape.Rectangle;
      }
      if (rbDrawCircle.Checked)
      {
      imageViewer1.ShapeSelected = ImageViewer.Shape.Circle;
      }
      if (rbDrawTriangle.Checked)
      {
      imageViewer1.ShapeSelected = ImageViewer.Shape.Triangle;
      }
      if (rbLine.Checked)
      {
      imageViewer1.ShapeSelected = ImageViewer.Shape.Line;
      }
      if (rbText.Checked)
      {
      imageViewer1.ShapeSelected = ImageViewer.Shape.Text;
      }
      }
      Console.WriteLine(imageViewer1.ShapeSelected.ToString());
      }

      Above is the code i used to check each radio button however i tried tidying it up with the bellow code and couldn't think of a way to set the enum depending on checked radio button.

      foreach (RadioButton rb in gbShape)
      {
      if (rb.Checked == true)
      {
      imageViewer1.ShapeSelected = ImageViewer.Shape.Circle; // How can make dynamic?
      }
      }

      Any ideas Thanx in advance

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      You can make use of Tag property of the radio buttons for this. While creating the buttons, set the Tag as enum value, like:

      rbSelectShape.Tag = ImageViewer.Shape.Select;

      Then in your foreach loop, use this:

      imageViewer1.ShapeSelected = rb.Tag; // Add appropriate casting

      50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

      G F 3 Replies Last reply
      0
      • D dan sh

        You can make use of Tag property of the radio buttons for this. While creating the buttons, set the Tag as enum value, like:

        rbSelectShape.Tag = ImageViewer.Shape.Select;

        Then in your foreach loop, use this:

        imageViewer1.ShapeSelected = rb.Tag; // Add appropriate casting

        50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

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

        Thank you for the response, that is a great idea and will make things shorter and easier to follow. Can also do this for the other group boxes on the form. Thanx George

        1 Reply Last reply
        0
        • D dan sh

          You can make use of Tag property of the radio buttons for this. While creating the buttons, set the Tag as enum value, like:

          rbSelectShape.Tag = ImageViewer.Shape.Select;

          Then in your foreach loop, use this:

          imageViewer1.ShapeSelected = rb.Tag; // Add appropriate casting

          50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

          F Offline
          F Offline
          freakyit
          wrote on last edited by
          #4

          In that case i would make a new class called ShapeRadioButton derived from RadioButton. in that new derived class i would implement a Property called Shape..? from type of your enum. using the tag would be a solution but i think my solution is not very difficult and would be better programming, think of it you want to use a second object than the tag object is used by you enum and the second gets what ?!? ^^ in the ShapeRadioButton you only need to implement the new property for the type and thats it. greetz :)

          G 1 Reply Last reply
          0
          • D dan sh

            You can make use of Tag property of the radio buttons for this. While creating the buttons, set the Tag as enum value, like:

            rbSelectShape.Tag = ImageViewer.Shape.Select;

            Then in your foreach loop, use this:

            imageViewer1.ShapeSelected = rb.Tag; // Add appropriate casting

            50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

            G Offline
            G Offline
            gwithey
            wrote on last edited by
            #5

            I have tried this solution however the following error occurs which i do not understand

            Error 1 foreach statement cannot operate on variables of type
            'System.Windows.Forms.GroupBox' because 'System.Windows.Forms.GroupBox'
            does not contain a public definition for 'GetEnumerator'

            The code is as follows:

                 // In Draw mode
                 if (rbDraw.Checked == true)
                 {
                    // Ceck each rb in Shape groupBox
                    foreach (RadioButton rb in gbShape)
                    {
                       if (rb.Checked == true)
                       {
                          // If checked shape selected = the tag (shape enum set in designer) of the button
                          imageViewer1.ShapeSelected = (FileScroller.ImageViewer.Shape)rb.Tag;
                       }
                    }
                 }
            
            F 1 Reply Last reply
            0
            • G gwithey

              I have tried this solution however the following error occurs which i do not understand

              Error 1 foreach statement cannot operate on variables of type
              'System.Windows.Forms.GroupBox' because 'System.Windows.Forms.GroupBox'
              does not contain a public definition for 'GetEnumerator'

              The code is as follows:

                   // In Draw mode
                   if (rbDraw.Checked == true)
                   {
                      // Ceck each rb in Shape groupBox
                      foreach (RadioButton rb in gbShape)
                      {
                         if (rb.Checked == true)
                         {
                            // If checked shape selected = the tag (shape enum set in designer) of the button
                            imageViewer1.ShapeSelected = (FileScroller.ImageViewer.Shape)rb.Tag;
                         }
                      }
                   }
              
              F Offline
              F Offline
              freakyit
              wrote on last edited by
              #6

              :D ok use

              // In Draw mode
              if (rbDraw.Checked == true)
              {
              // Ceck each rb in Shape groupBox
              foreach (Control ctrl in in gbShape.Controls)
              {
              if (ctrl is RadioButton)
              {
              if (rb.Checked == true)
              {
              // If checked shape selected = the tag (shape enum set in designer) of the button
              imageViewer1.ShapeSelected = (FileScroller.ImageViewer.Shape)rb.Tag;
              }
              }
              }
              }

              your RadioButtonClass:

              public class ShapeRadioButton : System.Windows.Form.RadioButton
              {
              private FileScroller.ImageViewer.Shape _shape;
              public FileScroller.ImageViewer.Shape Shape{
              get{return _shape;}
              set{_shape = value;}
              }

              G 1 Reply Last reply
              0
              • F freakyit

                In that case i would make a new class called ShapeRadioButton derived from RadioButton. in that new derived class i would implement a Property called Shape..? from type of your enum. using the tag would be a solution but i think my solution is not very difficult and would be better programming, think of it you want to use a second object than the tag object is used by you enum and the second gets what ?!? ^^ in the ShapeRadioButton you only need to implement the new property for the type and thats it. greetz :)

                G Offline
                G Offline
                gwithey
                wrote on last edited by
                #7

                I don't think i fully understand how that would work.

                class ShapeRadioButton : System.Windows.Forms.RadioButton
                {
                private ImageViewer.Shape m_shape = ImageViewer.Shape.Select;

                  public ImageViewer.Shape Shape
                  {
                     get
                     {
                        return m\_shape;
                     }
                     set
                     {
                        m\_shape = value;
                     }
                  }
                

                }

                i made the class ^ however it still needs to be set somewhere> do you mean the designer will set each RadioButton to have an instance of "ShapeRadioButton". Then use it in the same way as the tag Thax George

                1 Reply Last reply
                0
                • F freakyit

                  :D ok use

                  // In Draw mode
                  if (rbDraw.Checked == true)
                  {
                  // Ceck each rb in Shape groupBox
                  foreach (Control ctrl in in gbShape.Controls)
                  {
                  if (ctrl is RadioButton)
                  {
                  if (rb.Checked == true)
                  {
                  // If checked shape selected = the tag (shape enum set in designer) of the button
                  imageViewer1.ShapeSelected = (FileScroller.ImageViewer.Shape)rb.Tag;
                  }
                  }
                  }
                  }

                  your RadioButtonClass:

                  public class ShapeRadioButton : System.Windows.Form.RadioButton
                  {
                  private FileScroller.ImageViewer.Shape _shape;
                  public FileScroller.ImageViewer.Shape Shape{
                  get{return _shape;}
                  set{_shape = value;}
                  }

                  G Offline
                  G Offline
                  gwithey
                  wrote on last edited by
                  #8

                  That should work only problem being the rb doesnt exsit anymore and ctrl will not allow:

                  if (ctrl.Checked == true)

                  F 1 Reply Last reply
                  0
                  • G gwithey

                    That should work only problem being the rb doesnt exsit anymore and ctrl will not allow:

                    if (ctrl.Checked == true)

                    F Offline
                    F Offline
                    freakyit
                    wrote on last edited by
                    #9

                    ups sorry i forgot to cast the ctrl into a RadioButton

                    at rb.Checked use:
                    if (ctrl is RadioButton)
                    {
                    RadioButton rb = ctrl as RadioButton
                    if(rb.Checked)
                    {
                    ...
                    }
                    }

                    G 1 Reply Last reply
                    0
                    • F freakyit

                      ups sorry i forgot to cast the ctrl into a RadioButton

                      at rb.Checked use:
                      if (ctrl is RadioButton)
                      {
                      RadioButton rb = ctrl as RadioButton
                      if(rb.Checked)
                      {
                      ...
                      }
                      }

                      G Offline
                      G Offline
                      gwithey
                      wrote on last edited by
                      #10

                      Thank you for your help. This code now works perfectly, i can use it on the other group boxes on the form too.

                      // In Draw mode
                      if (rbDraw.Checked == true)
                      {
                      // Ceck each rb in Shape groupBox
                      foreach (Control ctrl in gbShape.Controls)
                      {
                      if (ctrl is RadioButton)
                      {
                      // Cast to RadioButton
                      RadioButton rb = ctrl as RadioButton;
                      if (rb.Checked == true)
                      {
                      // Set shape selected to the radioButton's tag (shape enum)
                      imageViewer1.ShapeSelected = (FileScroller.ImageViewer.Shape)rb.Tag;
                      }
                      }
                      }
                      }

                      Thank you again George

                      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