我有一个枚举
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;
}
}
答案 0 :(得分:2)
您的问题不是很清楚,但如果您有"Add"
之类的字符串,并且想要将其转换为Operation
,则可以使用Enum.Parse()
。类似的东西:
Operation op = (Operation)Enum.Parse(typeof(Operation), s);
但可能更好的选择是将单选按钮与枚举值直接关联,而不是通过按钮文本。具体如何取决于您使用的UI库类型。
答案 1 :(得分:1)
您可以将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);
}
}
答案 2 :(得分:0)
我假设您使用的是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
}
答案 3 :(得分:0)
我会轻松地将.Tag
控件添加到RadioButton
控件中,并使用Enum
中的名称,并在按钮更改时解析该事件上的字符串:
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);
...
}
}