将带有enum的组合框中的值符号化为另一个类
本文关键字:符号化 另一个 enum 组合 | 更新日期: 2023-09-27 18:26:30
我试图对包含枚举值的组合框中的选定值进行签名。
这是我填充combobox 的地方
cmbPrio.Items.AddRange(Enum.GetNames(typeof(PriorityType.Prioritytypes)));
cmbPrio.SelectedIndex = (int) PriorityType.Prioritytypes.Normal;
这是我尝试在类中设置值的地方
private void ReadInput()
{
if (String.IsNullOrEmpty(txtTodo.Text))
{
MessageBox.Show("Write something to do!");
}
else
{
currTasks.Priority = what code should go here??!
currTasks.Description = txtTodo.Text;
currTasks.TaskDate = dateTimePickerToDo.Value;
}
}
我的枚举类:
class PriorityType
{
public enum Prioritytypes
{
Very_Important,
Important,
Normal,
Less_Importan
}
}
我的课程是:
private PriorityType priority;
public PriorityType Priority
{
get
{
return this.priority;
}
set
{
this.priority = value;
}
}
直接用枚举值填充cmbPrio:
cmbPrio.Items.AddRange(Enum.GetValues(typeof(Prioritytypes)).Cast<object>().ToArray());
因此,您可以将其指定为枚举值:
currTasks.Priority = (Prioritytypes)cmbPrio.SelectedItem;
如果SelectedIndex与枚举类型后面的int值不对应,例如,这也会起作用
public enum Prioritytypes
{
Very_Important = 1,
Important = 2,
Normal = 4,
Less_Importan = 8
}
或者更改组合框中条目的顺序。