编辑基于枚举的属性的Trickey问题 [英] Trickey problem with editing enum based properties

查看:71
本文介绍了编辑基于枚举的属性的Trickey问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个问题,我之前没有碰到过。


我需要能够允许用户将下拉控件设置为特定的

颜色值。允许值来自Color枚举和

,遗憾的是我无法更改此实现细节。


我的问题是,如何加载值使用枚举进入下拉列表,

,更重要的是,当用户在下拉列表中选择一个条目时,如何确定

我将所选值重新调整为一个颜色来自枚举?


我真正想到的唯一解决方案是使用大量条件语句而非常混乱的方法

我认为必须有更优雅的方式。


很明显为什么使用Color enum实现它我不知道。我会亲自选择一个字符串。


非常感谢任何能提供如何处理此建议的人


Kindest Regards


thechaosengine

Hi all,

I have a problem that I havent really come accross before.

I need to be able to allow the user to set a dropdown control to a particular
colour value. The permissable values come from the Color enumeration and
unfortunately I can''t change this implementation detail.

My question is, how can I load the values into the dropdown using the enumeration,
and more importantly, when a user selects an entry in the dropdown, how do
I reconcile the selected value back into a Color from the enumeration?

The only solution that I could really think of was quite a messy approach
using lots of conditional statements and I''m thinking there must be a more
elegant way.

Quite why it was implemented using the Color enum I don''t know. I would have
chosen a string personally.

Many thanks to anybody who could offer advice on how to approach this

Kindest Regards

thechaosengine

推荐答案

>我的问题是,如何使用

枚举将值加载到下拉列表中,


Dim enumType As Type = GetType(Your_Enum)

Dim names()As String = [Enum] .GetNames(enumType)

Dim i As Integer


For i = 0 To name。长度 - 1

ComboBox1.Items.Add(姓名(i))

下一页
> My question is, how can I load the values into the dropdown using the
enumeration,

Dim enumType As Type = GetType(Your_Enum)
Dim names() As String = [Enum].GetNames(enumType)
Dim i As Integer

For i = 0 To names.Length - 1
ComboBox1.Items.Add(names(i))
Next
>更重要的是,当用户在下拉列表中选择一个条目,如何将所选值与枚举中的颜色进行协调?
and more importantly, when a user selects an entry in the dropdown, how do
I reconcile the selected value back into a Color from the enumeration?




''检索所选项的值。

Dim enumType As Type = GetType(Your_Enum)

Dim selection As String = DirectCast(ComboBox1.SelectedItem,String)

Dim value As频率= DirectCast([Enum] .Parse(enumType,选择),

Your_Enum)


希望这会有所帮助



''Retrieve value of the selected item.
Dim enumType As Type = GetType(Your_Enum)
Dim selection As String = DirectCast(ComboBox1.SelectedItem, String)
Dim value As Frequency = DirectCast([Enum].Parse(enumType, selection),
Your_Enum)

Hope this helps


使用静态方法GetNames()和GetValues。


Array zh umValues = Enum.GetValues(enumType);

string [] enumNames = Enum.GetNames(enumType);


for(int i = 0; I< enumNames.Length; ++ i)

{

comboBox.AddItem(enumNames [i]);

addedItem.Tag = enumValues.GetValue(i); //将值与

关联到该项目。

}


然后您可以简单地将所选项目的标记字段强制转换回来到

枚举。如果您对使用标记字段感到不舒服,可以使用

Enum.Parse()并传递所选项目的文本。


问候

Senthil

Use the static method GetNames() and GetValues.

Array enumValues = Enum.GetValues(enumType);
string [] enumNames = Enum.GetNames(enumType);

for (int i = 0; i<enumNames.Length; ++i)
{
comboBox.AddItem(enumNames[i]);
addedItem.Tag = enumValues.GetValue(i);//Associate value with
that item.
}

You can then simply cast the Tag field of the selected item back to the
enum. If you don''t feel comfortable using the Tag field, you can use
Enum.Parse() and pass the text of the selected item.

Regards
Senthil


thechaosengine写道:

thechaosengine wrote:

很明白为什么它是使用Color enum实现的,我不知道。我个人会选择一个字符串。

非常感谢任何可以提供如何处理此建议的人
Quite why it was implemented using the Color enum I don''t know. I
would have chosen a string personally.

Many thanks to anybody who could offer advice on how to approach this




颜色不是枚举,它是一个结构。


你可以用反射做你想做的事,找公共

类型为Color的静态属性,或者或者你可以使用

KnownColor枚举即


private void button1_Click(object sender,System.EventArgs e)

{

int [] allColors =(int [])Enum.GetValues(typeof(KnownColor));

foreach(int in allColors)

{

颜色col = Color.FromKnownColor((KnownColor)i);

comboBox1.Items.Add(col.Name);

}

}


我倾向于创建一个包装类,用

颜色加载它,重载ToString( )方法来显示颜色名称和

将整个东西存储在组合中,而不是用

ta来粘贴g property。

问Tim。



Color is not an enum, its a struct.

You could use reflection to do what you want, look for the public
static properties of type Color, or alternatively you could use the
KnownColor enumeration i.e.

private void button1_Click(object sender, System.EventArgs e)
{
int[] allColors = (int[])Enum.GetValues(typeof(KnownColor));
foreach(int i in allColors)
{
Color col = Color.FromKnownColor((KnownColor)i);
comboBox1.Items.Add(col.Name);
}
}

I would be inclined to create a wrapper class, load it up with the
color, overload the ToString() method to display the color name and
store the whole thing in the combo, rather than muck around with the
tag property.
Regards Tim.


这篇关于编辑基于枚举的属性的Trickey问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆