Doesn't appear that a radio button has a value property other than true/false so I was hoping someone could help me out with this one.

I'm writing a method applied to the "CheckedChanged" event. The method is applied to all of the radio buttons, so it always triggers regardless of what button is checked.

The issue is, each button applies a specific filter to the data displayed on the form and I am having problems determining which filter to apply because determining which radiobutton (of the 3) is actually in the checked state.

I know I could always use the name of the checkbox or the text associated with it, but those are rather arbitrary values in the whole scheme of things (in other words, the control names or text values could change before the project is complete and the change might be missed in the code) so I was hoping I could apply a static (or close to static) value to each radio button besides using the name, text or tag property. Any suggestions?

Recommended Answers

All 6 Replies

Tag is your best bet. As for determining which one was selected, it's easy:

private void radioButton_CheckChanged(object sender, EventArgs e) {
    RadioButton rb = sender as RadioButton;
}

'rb' is the one that was selected. You can now access all its properties, etc. without actually knowing which one it is :)

Momerath,

Yeah, my method uses the same yours does in your example. I think I'm just spoiled with the IDE and for some reason don't think outside the box now when it comes to controls on a form.

I'm going to throw this out there and please, point and laugh if it's a bad idea... but is it possible that while a form is being layed-out, to add a reference to the radio buttons in a specific container (in this case a tab control page) to a dictionary object, or some type of array? With the assumption that the dictionary/array would ALWAYS add the references in the same order?

Just a thought. Otherwise I think I'll use the tag control, though I don't really like it much.

Yes, that would work, and with an array you can force to the control to a specific spot (other collections don't ensure that they will be in the order you added them, but usually they are, except Queue which has the sole purpose of making sure they are in the order you added them).

I do think you are making more work than you need to. What is your objection to using Tag?

It's a personal hang-up. Tag is one of those properties I never use on a object. I just need to get over it I think.

I have the same hangup with using "reflection" for anything. For some reason I keep thinking that using reflection is a very bad idea, and if your only solution is to use that, you shouldn't be doing what your doing.

Meh, now that I think of it, no wonder my co-workers look at me like I'm crazy most of the time!

I am just a stickler for efficiency and standards. My mentality is "Just because tag and reflection are there, doesn't mean you should use it."

Oh well, I'll just go with the tag. Thanks! You always seem to be so helpful! Too bad you can't nominate users as MVPs or something :)

I know that this is solved but another alternative is to create your own derived control and add any additional properties, methods or delegates you need.

public class MyDataClass
	{
		// define class here
	}

	public class MyRadioButton : RadioButton
	{
		public MyDataClass DataItem { get; set; }
	}

Once you have built your application your new control should be available in the ToolBox to use on any of your forms.

Ooooh!

I like that!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.