How do I display Enum Properties in a Dropdownlist?
I'm m开发者_如何学Pythonaking a simple custom control and I would like to have a dropdownlist
of defined custom properties and I'm perplexed as to why the code below fails to display the enum
in the properties list of the combobox.
How do I display enum
values in a Dropdownlist?
public partial class MyComboBox : ComboBox
{
public enum Multipliers { B = 1, KB = 2, MB = 10, GB = 20, TB = 30 } { get; set; }
public string SuperType { get; set; }
public bool Global { get; set; }
}
You're not showing any code relating to how you're putting those enum values INTO the actual display list. You have to point the ComboBox at your enum as a DataSource (technically you have to point it at a call to Enum.GetValues(typeof(Multipliers))
), or manually manipulate the Items collection, to get your values into the list.
Here's what you need to do:
foreach (var item in Enum.GetValues(typeof(Multipliers )))
{
ComboBox1.Items.Add(item);
}
The enum is a type within MyComboBox, not a property.
You could create a property like this:
public partial class MyComboBox : ComboBox
{
public enum Multipliers { B = 1, KB = 2, MB = 10, GB = 20, TB = 30 }
public string SuperType { get; set; }
public bool Global { get; set; }
public Multipliers myMultiplierProperty {get; set;}
}
As to how those properties show up in the combo box depends a lot on the code that puts them in there. Since you haven't given us that, I can't help if the problem is in there.
精彩评论