识别使用枚举作为值检查哪个单选按钮 [英] recognize which radio button is checked using enum as value

查看:72
本文介绍了识别使用枚举作为值检查哪个单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使用 WinForms.我有一个 GroupBox,它包装了三个 RadioButton.我使用设计视图添加了它们,并在构造函数中将每个按钮标记为相应的枚举值,如

I'm struggling with WinForms. I have a GroupBox which wraps three RadioButtons. 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;
}

在我的班级中,我有 Dictionary 类型的属性属性,它使用这个枚举作为键,所以我希望当用户点击 winform 按钮时识别哪个单选按钮被选中并分配给那个字典.

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);

我是否需要使用 switch 语句来识别检查了哪个 Enum 或者有更好的方法吗?

Do I need to use switch statement to recognize which Enum is checked or is there some better way?

推荐答案

如果您设置了 Tag,您可以在需要时随时取回它.请注意,您需要将其转换为原始类型.类似的东西:

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;

这篇关于识别使用枚举作为值检查哪个单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆