简体   繁体   English

C# 中的枚举和组合框

[英]Enums and ComboBoxes in C#

I am currently developing a C# application.我目前正在开发一个 C# 应用程序。

I need to use an Enum with a ComboBox to get the selected month.我需要使用带有 ComboBox 的 Enum 来获取所选月份。 I have the following to create the Enum:我有以下内容来创建枚举:

enum Months 
{ 
   January = 1,
   February,
   March,
   April,
   May,
   June,
   July,
   August,
   September,
   October,
   November,
   December 
};

I then initialise the ComboBox using the following:然后我使用以下方法初始化 ComboBox:

cboMonthFrom.Items.AddRange(Enum.GetNames(typeof(Months)));

This bit of code works fine however the problem is when I try to get the selected Enum value for the selected month.这段代码工作正常,但问题是当我尝试获取所选月份的所选 Enum 值时。

To get the Enum value from the ComboBox I have used the following:为了从 ComboBox 中获取 Enum 值,我使用了以下内容:

private void cboMonthFrom_SelectedIndexChanged(object sender, EventArgs) 
{
   Months selectedMonth = (Months)cboMonthFrom.SelectedItem;
   Console.WriteLine("Selected Month: " + (int)selectedMonth);
}

However, when I try to run the code above it comes up with an error saying A first chance exception of type 'System.InvalidCastException' occurred .但是,当我尝试运行上面的代码时,它出现了一个错误,说A first chance exception of type 'System.InvalidCastException' occurred

What I have done wrong.我做错了什么。

Thanks for any help you can provide感谢您的任何帮助,您可以提供

Try this尝试这个

Months selectedMonth = (Months)Enum.Parse(typeof(Months), cboMonthFrom.SelectedItem.ToString());

instead of代替

Months selectedMonth = (Months)cboMonthFrom.SelectedItem;

Updated with correct changes更新了正确的更改

The issue is that you're populating combobox with string names ( Enum.GetNames returns string[] ) and later you try to cast it to your enum.问题是您正在使用字符串名称填充组合框( Enum.GetNames返回string[] ),然后您尝试将其转换为您的枚举。 One possible solution could be:一种可能的解决方案是:

Months selectedMonth = (Months)Enum.Parse(typeof(Months), cboMonthFrom.SelectedItem);

I would also consider using existing month information from .Net instead of adding your enum:我还会考虑使用 .Net 中现有的月份信息,而不是添加您的枚举:

var formatInfo = new System.Globalization.DateTimeFormatInfo();

var months = Enumerable.Range(1, 12).Select(n => formatInfo.MonthNames[n]);

Try尝试

Months selectedMonth = 
    (Months) Enum.Parse(typeof(Months), cboMonthFrom.SelectedItem);

There is really no reason to use Enum.GetNames at all.根本没有理由使用Enum.GetNames Why store strings in the ComboBox if you actually want the months?如果您真的想要月份,为什么要在 ComboBox 中存储字符串?

Just use Enum.GetValues instead:只需使用Enum.GetValues代替:

foreach (var month in Enum.GetValues(typeof(Months)))
    cboMonthFrom.Items.Add(month);

[...]

// This works now
Months selectedMonth = (Months)cboMonthFrom.SelectedItem;

You've stored the names of the months in the combobox, not the int values.您已经在组合框中存储了月份的名称,而不是 int 值。 Your selected item will be a string.您选择的项目将是一个字符串。

Here is my one line solution:这是我的单行解决方案:

comboBox1.DataSource = Enum.GetValues( typeof( DragDropEffects ) ); comboBox1.DataSource = Enum.GetValues( typeof( DragDropEffects ) );

This will get you the selected item:这将为您提供所选项目:

DragDropEffects effects = ( DragDropEffects ) comboBox1.SelectedItem; DragDropEffects 效果 = ( DragDropEffects ) comboBox1.SelectedItem;

Note: You can use any Enum that you like including your own.注意:您可以使用任何您喜欢的 Enum,包括您自己的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM