views:

46

answers:

2

I have 16 radio buttons in an application of mine.. I have to set a variable based on which one was selected.. and I've produced some very ugly code doing this..

private void Foo_CheckedChanged(object sender, EventArgs e)
    {
        convertSource = 1;
    }

private void Bar_CheckedChanged(object sender, EventArgs e)
    {
        convertSource = 2;
    }

private void Baz_RadioButton_CheckedChanged(object sender, EventArgs e)
    {
        convertSource = 3;
    }

Now, I've been thinking about it and to be honest I thought there might have been a way to do it with a switch.. I just can't conceptualize it in my mind.

If anyone could show me a more efficient way of doing this, I'd really appreciate it. It's just really bugging me that such a simple thing as this is taking up fifty to seventy lines of code.

thanks, cpatton

A: 

Personally, I hate radio buttons if there are more than 2 or 3 choices - a ComboBox is a better control for picking one option from many.

However, a quick fix to your problem is to put the convertSource value of each radio button into that control's Tag property (do this in the designer). Add this method to your form:

private void rb_CheckedChanged(object sender, EventArgs e) 
{ 
    convertSource = (int)((RadioButton)sender).Tag;
}

In your form's load event (assuming these radio buttons are all on a group box), assign the handler like this:

foreach (RadioButton rb in groupBox1.Controls)
{
    rb.CheckedChanged += rb_CheckedChange;
}

Make sure you remove all the event handlers that you added manually (like Foo_CheckedChanged).

MusiGenesis
Ah, thank you! I don't know why I decided to use a radiobutton for this certain task.. a combo box makes so much more sense, so I think i'll go with that.thank you, i really appreciate your time and effort.
C Patton
A: 

You can bind the same handler to more than one event. You can write one handler and check which is the sender:

private void RadioButton_CheckedChanged(object sender, EventArgs e)
{
    if (sender == Foo_RadioButton) {
        convertSource = 1;
    } else if (sender == Bar_RadioButton) {
        convertSource = 2;
    } else if (sender == Baz_RadioButton) {
        convertSource = 3;
    } else {
        // Error
    }
}

or even more concisely:

private void RadioButton_CheckedChanged(object sender, EventArgs e)
{
    List<RadioButton> radioButtons = new List<RadioButton> {
        Foo_RadioButton,
        Bar_RadioButton,
        Baz_RadioButton
    };

    int index = radioButton.IndexOf((RadioButton)sender);
    convertSource = index + 1;
}        

You can bind this handler to the CheckedChanged event in the properties window for each radio button.

Mark Byers
Ah, thanks a lot! I'm going to actually go with a combobox for this part of my app as it makes a lot more sense.. I hadn't thought about using just one handler and checking the sender, though. That's a great idea, and I appreciate it tons. Will probably use it for another section of my app :)
C Patton