运行时枚举 [英] Enumeration at Runtime
问题描述
人们.
通过使用PropertyGrid控件,我遇到了一些问题.
假设我有一个带有某些已定义属性的已定义类的对象.这些属性之一的定义如下:
Hi people.
I''ve faced some problems through working with the PropertyGrid control.
Suppose that I have an object of a defined class with some defined properties. One of these properties is defined like this:
public enum Intervals
{
Free,
Used,
New
}
private Intervals _int;
public Intervals MyList
{
get { return _int; }
set { _int = value; }
}
当我将带有PropertyGrid控件的此类的对象分配为"selectedobject"属性时,如您所知,我可以在PropertyGrid控件的组合框中看到免费",已使用"和新建"项.
但问题在这里:
如何在运行时向这些值添加(或删除)项目?
或
如何在运行时向枚举添加(或删除)值?
请让我高兴.
When I assign an object of this class with a PropertyGrid control as "selectedobject" attribute, I can see "Free", "Used" and "New" items in a combobox in the PropertyGrid control, as you know.
But Problem is here:
How could I add (or remove) items to these values at runtime?
or
How could I add (or remove) values to an enumeration at runtime?
Make me happy, please.
推荐答案
我会使用Dictionary
:
代替List
Instead of aList
, I''d use aDictionary
:
Dictionary<string, int> enumerator = new Dictionary<string, int>();
enumerator.Add("enum1", 1);
您可以使用多种类型的集合.最好的选择是在Google上搜索"c# collections
",然后选择最符合您要求的内容.
There are many types of collections you can use. Your best bet is to google "c# collections
" and pick the one that actually fits your requirements.
John的答案来使用Dictionary<>将在类级别为您工作,但不会像您期望的那样在属性网格中自动工作.因此,要在运行时从属性网格添加/编辑/删除项目,您需要使用属性网格可用的UI扩展功能编写一些自定义UI代码.
John''s answer to use a Dictionary<> will work for you at the class level, but it will not automatically work as you expect with the property grid. So to add/edit/remove items at runtime from the property grid, you''d need to write some custom UI code using the UI extension features available for the property grid.
这篇关于运行时枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!