Click here to Skip to main content
15,888,301 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So I have a method in my Form class to check which RadioButtons in a GroupBox are selected, and thus return a string.

I then create an instance of Form in another class like this

C#
Form1 form = new Form1();



I then use that object to call my method from the form class

C#
form.whatChecked();


This is my method in the form class

C#
public String whatChecked()
        {
            if (a1.Checked)
            {
                return "a1";
            }
            else if (a2.Checked)
            {
                return "a2";
            }
            else if (a2.Checked)
            {
                return "a3";
            }
            else if (a2.Checked)
            {
                return "a4";
            }
            else if (a2.Checked)
            {
                return "a5";
            }
            else if (a2.Checked)
            {
                return "a6";
            }
            else
            {
                return null;
            }
        }


I've made a check in the other class, and I see that it always returns the "else" value (In this case, Null). I've tested to make sure by having the else return a String, and it does indeed return, even though a1 or a2 are checked.
Posted
Comments
Philippe Mori 23-Aug-15 21:22pm    
You should represent your options with an enum. It will be a more maintainable design. Ideally you would also have the mapping between the control and the enumeration value at only once place. That way, if you add options, you only have to do a few localized modifications.

You seem to check the same radio button in most of the cases. Should the code look like:
C#
public String whatChecked()
        {
            if (a1.Checked)
            {
                return "a1";
            }
            else if (a2.Checked)
            {
                return "a2";
            }
            else if (a3.Checked)
            {
                return "a3";
            }
            else if (a4.Checked)
            {
                return "a4";
            }
            else if (a5.Checked)
            {
                return "a5";
            }
            else if (a6.Checked)
            {
                return "a6";
            }
            else
            {
                return null;
            }
        }


ADDED:

C#
private void radioButtons_CheckedChanged(Object sender, EventArgs e)
{
   if (a1.Checked))
   {
      // here execute the code what needs to be done when a1 is selected
      // in the same event handler also add code for other radio buttons since 
      // this event handler will be used for all radio buttons
   }
}


And in the Form_Load event or constructor, wire all the check boxes to use the same event handler

C#
this.a1.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
this.a2.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
this.a3.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
this.a4.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
...
 
Share this answer
 
v2
Comments
Ridwan Sameer 22-Aug-15 14:48pm    
Hi, Terribly sorry I had copy pasted an old code.

Yes the code should look like that. However it still does not work

In my other class I call the method and have a check like this

Form1 form = new Form();
parkingspot = form.whatChecked();
if (String.Equals(parkingspot,"a1")){
colour = Color.Gray;
}

Basically, I have a program that prints a moving rectangle of a specific color. So I want it to be such that if a1 is checked, the color will be Gray. However it does not work, but if I changed the else statement in the whatChecked() method to return "a1" it does work, which proves that the method only returns from the else statement, even if a radiobutton is checked.
Wendelius 22-Aug-15 15:05pm    
OK I see, but if the method returns say "a5", how do you handle that? Based on the code fragment you only change the color in case of "a1".
Ridwan Sameer 22-Aug-15 15:15pm    
Yes, in this specific case I just set it to handle a1 to sort of test out an idea I plan to flesh out later.

I need to be able to get the value of the RadioButton into my current class from the Form Class to build upon my idea. So I basically set it to change the color to Gray if a1 is selected to see if it works.

Ralf Meier 22-Aug-15 16:35pm    
Based on which Event do you call the Method (whatChecked) ?
I would prefer to connect the Event CheckedChanged to each Radiobutton and after this you only have to identify the sender ...
Ridwan Sameer 22-Aug-15 23:34pm    
I'm sorry I don't exactly get what you mean.
I don't call the method based on an event. I just call it to return a strong based on what is checked, making use of the "radiobutton.Checked()" method, which I was informed would return true or false based on whether it was checked or not.

I read about this checkedChanged event, currently all my checkedChanged events are empty. Could you provide some snippets based on this scenario as to how I could implement it?
This code I have taken from the posted Solution from Mika to change it a little bit (I hope you will forgive me Mika).

C#
private void radioButtons_CheckedChanged(Object sender, EventArgs e)
{
   RadioButton mySender = sender ;
   if (mySender.Checked) && (mySender.Name="A1")
   {
      // here execute the code what needs to be done when a1 is selected
      // in the same event handler also add code for other radio buttons since 
      // this event handler will be used for all radio buttons
   }
}


this could also look like this :
C#
private void radioButtons_CheckedChanged(Object sender, EventArgs e)
{
	if (! object.ReferenceEquals(Sender.gettype, typeof(radiobutton))) return;  // if incomming object is not radiobutton


radiobutton mySender = Sender;
if (mySender.Checked) 
   {

   switch (mySender.name) {
	case "a1":
                // do the Action here
		break;
	case "a2":
                // do the Action here
		break;
      }
   }
}
 
Share this answer
 
v2
Comments
Philippe Mori 23-Aug-15 21:15pm    
If you need to test the caller and do specific action for each cases, then you should use one handler per control.
Ralf Meier 24-Aug-15 0:22am    
I'm not sure what you tried to tell me.
But nevertheless - I have improved my solution ... perhaps it is interesting for the inquirer.
Philippe Mori 24-Aug-15 7:46am    
Why shared an event handler if the code then switches on the control name to do the action.
Ralf Meier 28-Aug-15 5:57am    
That is very difficult to answer.
Perhaps you think about a Control that contents severall dynamic created (sub-)Controls. In this case you have (my opinion) only this possibility.
Or .. (in this case) you want to have to things which belongs together also be programmed together.

In my opinion there is no generell way to solve this. But I think it could be a mistake to say "do it only like this".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900