How to check a groupbox for checked radiobuttons dynamicaly
-
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
-
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
You can make use of
Tag
property of the radio buttons for this. While creating the buttons, set theTag
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...!!
-
You can make use of
Tag
property of the radio buttons for this. While creating the buttons, set theTag
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...!!
-
You can make use of
Tag
property of the radio buttons for this. While creating the buttons, set theTag
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...!!
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 :)
-
You can make use of
Tag
property of the radio buttons for this. While creating the buttons, set theTag
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...!!
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; } } }
-
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; } } }
: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;}
} -
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 :)
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
-
: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;}
} -
That should work only problem being the rb doesnt exsit anymore and ctrl will not allow:
if (ctrl.Checked == true)
-
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)
{
...
}
}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