[英]return enum value based on selection
我有一个枚举
public enum Operation
{
Add = 1,
Substract = 2,
Multiply = 3,
Divide = 4
}
我有四个单选按钮:加,减,乘,除。
基于选择,我想返回相应的Enum值。 我所有的单选按钮都在一个组框中。
我知道这是一件简单的事情,但是长期以来我一直无法正确解决。 谢谢。
编辑
那就是我尝试过的...
public Operation Operation
{
get
{
foreach (Control control in gbxOperation.Controls)
{
var radioButton = control as radioButton;
if (radioButton != null && radioButton.Checked)
{
if(radioButton.Text.ToLower() == "add")
return Operation.Add;
if (radioButton.Text.ToLower() == "subtract")
return Operation.Substract;
if (radioButton.Text.ToLower() == "multiply")
return Operation.Multiply;
if (radioButton.Text.ToLower() == "divide")
return Operation.Divide;
}
}
return Operation.Add;
}
}
从您的问题中还不清楚,但是如果您有一个类似"Add"
的字符串,并且想要将其转换为Operation
,则可以使用Enum.Parse()
。 就像是:
Operation op = (Operation)Enum.Parse(typeof(Operation), s);
但是可能更好的选择是直接将单选按钮与枚举值相关联,而不是通过按钮的文本。 具体如何执行取决于您使用的是哪种UI库。
为此,可以使用RadioButton的Tag属性:
在InitializeComponent()之后的构造函数中:
addButton.Tag = Operation.Add;
subtractButton.Tag = Operation.Subtract;
addButton.Tag = Operation.Multiply;
addButton.Tag = Operation.Divide;
public Operation Operation
{
get
{
RadioButton checkedButton = gbxOperation.Controls.OfType<RadioButton>().
Where(button => button.Checked).First();
return (Operation)(checkedButton.Tag);
}
}
我假设您正在使用Windows窗体。 在下面的示例中,我可以使用两种方法:
使用Tag属性(用于自定义用户数据)。 然后,您可以将Tag值强制转换为枚举的单选按钮的枚举。
或者,只需在单选按钮和枚举之间创建一个字典。 您可以将单选按钮用作键,将操作枚举用作值。
问候。
public class MyForm : Form
{
public MyForm()
{
InitializeComponent();
// Method 1) The Tag property on any control can be user as user data
radioButton1.Tag = Operation.Add;
radioButton2.Tag = Operation.Subtract;
radioButton3.Tag = Operation.Multiply;
radioButton4.Tag = Operation.Divide;
// Method 2) Use a Dictionary
radioButtonToOperation = new Dictionary<RadioButton, Operation>
{
{ radioButton1, Operation.Add },
{ radioButton2, Operation.Subtract },
{ radioButton3, Operation.Multiply },
{ radioButton4, Operation.Divide },
};
}
// Fields
GroupBox groupBox1;
RadioButton radioButton1;
RadioButton radioButton2;
RadioButton radioButton3;
RadioButton radioButton4;
Dictionary<RadioButton, Operation> radioButtonToOperation;
private void InitializeComponent()
{
groupBox1 = new GroupBox();
radioButton1 = new RadioButton();
radioButton2 = new RadioButton();
radioButton3 = new RadioButton();
radioButton4 = new RadioButton();
groupBox1.Text = "Operations";
groupBox1.Dock = DockStyle.Fill;
radioButton1.Text = "Add";
radioButton1.Dock = DockStyle.Top;
radioButton1.CheckedChanged += radioButtons_CheckedChanged;
radioButton2.Text = "Subtract";
radioButton2.Dock = DockStyle.Top;
radioButton2.CheckedChanged += radioButtons_CheckedChanged;
radioButton3.Text = "Multiply";
radioButton3.Dock = DockStyle.Top;
radioButton3.CheckedChanged += radioButtons_CheckedChanged;
radioButton4.Text = "Divide";
radioButton4.Dock = DockStyle.Top;
radioButton4.CheckedChanged += radioButtons_CheckedChanged;
groupBox1.Controls.Add(radioButton4);
groupBox1.Controls.Add(radioButton3);
groupBox1.Controls.Add(radioButton2);
groupBox1.Controls.Add(radioButton1);
Controls.Add(groupBox1);
}
void radioButtons_CheckedChanged(object sender, EventArgs e)
{
RadioButton radioButton = sender as RadioButton;
if (radioButton == null) return; // Not a radio button
if (!radioButton.Checked) return; // Radio button isn't checked
// Method 1) Get the Operation from the Tag property
Operation? operationFromTag = GetOperationFromTag(radioButton);
Console.WriteLine("From Tag: " + (operationFromTag == null ? "(none)" : operationFromTag.ToString() ));
// OR Method 2) Get the Operation from the Dictionary
Operation? operationFromDictionary = GetOperationUsingDictionary(radioButton);
Console.WriteLine("From Dictionary: " + (operationFromDictionary == null ? "(none)" : operationFromDictionary.ToString()) );
}
private Operation? GetOperationFromTag(RadioButton radioButton)
{
if (radioButton == null) return null;
if (radioButton.Tag is Operation)
{
Operation operationFromTag = (Operation)radioButton.Tag;
return operationFromTag;
}
return null;
}
private Operation? GetOperationUsingDictionary(RadioButton radioButton)
{
if (radioButton == null) return null;
Operation operationFromDictionary;
return
radioButtonToOperation.TryGetValue(radioButton, out operationFromDictionary)
? operationFromDictionary
: (Operation?)null;
}
public Operation? SelectedOperation
{
get
{
// You must include System.Linq in your using block at the top of the file for the following
// extension methods to be picked up by the compiler:
// * Enumerable.OfType<T> is an extension method on IEnumerable.
// * Enumerable.SingleOrDefault<T> is an extension method on IEnumerable<T>
RadioButton selectedRadioButton =
groupBox1.Controls // Go through each of the groupbox child controls
.OfType<RadioButton>() // Need to convert Controls, which is IEnumerable, to IEnumerable<Control>
.Where(radioButton => radioButton.Checked) // Filter through only the checked radio buttons
.SingleOrDefault(); // Get the single result, or none. (Exception if there are more than one result)
return GetOperationUsingDictionary(selectedRadioButton);
}
}
}
public enum Operation
{
Add = 1,
Subtract = 2,
Multiply = 3,
Divide = 4
}
我将通过将字符串.Tag
与Enum
的名称添加到RadioButton
控件并在按钮更改时在事件上解析该字符串的简单组合:
public Operation Operation { get; private set; }
private void rb_CheckedChanged(object sender, EventArgs e)
{
var rb = sender as RadioButton;
if (rb.Checked)
{
this.Operation = (Operation)Enum.Parse(typeof(Operation), rb.Tag as string);
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.