Reputation: 3515
I'm struggling with WinForms. I have a GroupBox
which wraps three RadioButton
s. I added them using the design view and inside the constructor I tag every button to corresponding enum value like
public MyApp()
{
radioBtnBasic.Tag = UserChoiceEnum.Basic;
radioBtnLite.Tag = UserChoiceEnum.Lite;
radioBtnStandard.Tag = UserChoiceEnum.Standard;
}
Inside my class I have property property of type Dictionary
which uses this enum as a key, so I want when the user clicks on winform button to recognize which radio button is checked and to assign to that dictionary.
I've found how to fetch checked option
var choice = grpBox1.Controls.OfType<RadioButton>().FirstOrDefault(x => x.Checked);
Do I need to use switch statement to recognize which Enum is checked or is there some better way?
Upvotes: 2
Views: 4619
Reputation: 40
created enum :
public enum SearchType
{
ReferenceData = 0,
Trade = 1,
}
Then use SelectedIndexChanged
event of radioGroup
control.
private void RadioTypes_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.RadioTypes.SelectedIndex < 0) return;
SearchType n = (SearchType)this.RadioTypes.SelectedIndex;
switch (n)
{
case SearchType.ReferenceData:
break;
case SearchType.Trade:
break;
}
}
Upvotes: 0
Reputation: 9394
You get the UserChoiceEnum by:
RadioButton choice = grpBox1.Controls.OfType<RadioButton>().FirstOrDefault(x => x.Checked);
UserChoiceEnum userChoice = (UserChoiceEnum)choice.Tag;
Upvotes: 6
Reputation: 100547
If you set Tag
you can simply get it back whenever you need. Note that you need to cast it to original type. Something like:
var choiceAsEnum = (UserChoiceEnum)choice.Tag;
Upvotes: 2