Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取与枚举绑定的组合框的选定值?_C# - Fatal编程技术网

C# 如何获取与枚举绑定的组合框的选定值?

C# 如何获取与枚举绑定的组合框的选定值?,c#,C#,我正在使用枚举类型绑定组合框。 我想在组合框的选定索引更改时获取枚举的选定值 我试着这样做,但没有用 枚举是这样的 CategoryType { T=1, D, S } 这是我填充组合框的方式 custCmb.DataSource = Enum.GetNames(typeof(CategoryType)); 选定的索引更改事件如下所示 private void custCmb_SelectedIndexChanged(object sender, EventArg

我正在使用枚举类型绑定组合框。
我想在组合框的选定索引更改时获取枚举的选定值

我试着这样做,但没有用

枚举是这样的

CategoryType
{
    T=1, 
    D, 
    S
}
这是我填充组合框的方式

custCmb.DataSource = Enum.GetNames(typeof(CategoryType));
选定的索引更改事件如下所示

private void custCmb_SelectedIndexChanged(object sender, EventArgs e)
{
   categoryType selCustomizationType = Enum.Parse(CategoryType, custCmb.SelectedValue);
}

但是上面的方法不起作用,我需要它的数值。

对于必须使用的绑定:

custCmb.DataSource = Enum.GetValues(typeof(CategoryType));
并具有所选的值:

private void custCmb_SelectedIndexChanged(object sender, EventArgs e)
{
   CategoryType selCustomizationType = (CategoryType)custCmb.SelectedValue;
}

对于绑定,您必须使用:

custCmb.DataSource = Enum.GetValues(typeof(CategoryType));
并具有所选的值:

private void custCmb_SelectedIndexChanged(object sender, EventArgs e)
{
   CategoryType selCustomizationType = (CategoryType)custCmb.SelectedValue;
}

改用
selectedText

private void custCmb_SelectedIndexChanged(object sender, EventArgs e)
{
   CategoryType selCustomizationType = Enum.Parse(CategoryType, custCmb.SelectedText);
}

改用
selectedText

private void custCmb_SelectedIndexChanged(object sender, EventArgs e)
{
   CategoryType selCustomizationType = Enum.Parse(CategoryType, custCmb.SelectedText);
}

我已经测试过了,效果很好。你需要在这里做一些改变

首先,您需要使用如下值进行绑定

custCmb.DataSource = Enum.GetValues(typeof(CategoryType));
然后您可以将所选的一个作为

private void custCmb_SelectedIndexChanged(object sender, EventArgs e)
{
    CategoryType selCustomizationType = (CategoryType)custCmb.SelectedValue;
    int result = (int)selCustomizationType;
}
枚举是数字的

GetNames
将返回包含字段名的字符串数组


GetValues
将返回一个int数组

我已经测试过了,它工作正常。你需要在这里做一些改变

首先,您需要使用如下值进行绑定

custCmb.DataSource = Enum.GetValues(typeof(CategoryType));
然后您可以将所选的一个作为

private void custCmb_SelectedIndexChanged(object sender, EventArgs e)
{
    CategoryType selCustomizationType = (CategoryType)custCmb.SelectedValue;
    int result = (int)selCustomizationType;
}
枚举是数字的

GetNames
将返回包含字段名的字符串数组


GetValues
将返回一个int数组

如果执行此操作,则可以强制转换它:

CategoryType selCustomizationType =(CategoryType)Enum.Parse(typeof(CategoryType), custCmb.SelectedValue);

如果执行以下操作,您可以直接将其投射:

CategoryType selCustomizationType =(CategoryType)Enum.Parse(typeof(CategoryType), custCmb.SelectedValue);


如何填写组合框?您将它绑定到什么?能否尝试
CategoryType selCustomizationType=(CategoryType)custCmb.SelectedValue请?强制转换为:
(CategoryType)Enum.Parse(CategoryType,custCmb.SelectedValue)@lcarus它告诉我们:'libEPOS.CategoryType'是一个'type',但它像一个'variable'一样使用。@nearyottodotnet看看我的答案。您忘记调用
typeof(CategoryType)
如何填充组合框?您将它绑定到什么?能否尝试
CategoryType selCustomizationType=(CategoryType)custCmb.SelectedValue请?强制转换为:
(CategoryType)Enum.Parse(CategoryType,custCmb.SelectedValue)@lcarus它告诉我们:'libEPOS.CategoryType'是一个'type',但它像一个'variable'一样使用。@nearyottodotnet看看我的答案。您忘了调用
typeof(CategoryType)
SelectedTExt始终是一个字符串,不能强制转换为此感到抱歉@ToDotNet,fixed@NoviceToDotNet请看下面我的答案。SelectedTExt始终是一个字符串,不能使用。对此表示担忧@ToDotNet,fixed@NoviceToDotNet查看下面我的答案。在看到初始化代码后刚刚编辑查看我标记为关闭的发布链接,以查找可能的重复。。你正在寻找的答案就在那里..在看到初始化代码后刚刚编辑查看我标记为关闭的发布链接以查找可能的重复。。你要找的答案就在那里。它给了我最高的分数,但我不能得到1的值吗toppiong@NoviceToDotNet查看我的更新int结果=(int)selCustomizationType;
OP需要理解ORDINAL
的含义。从另一个角度看,你的答案看起来不错,GetValus是如何在GetName@NoviceToDotNet更新了我的答案它给了我最好的答案,但是我不能得到1的值吗toppiong@NoviceToDotNet查看我的更新int结果=(int)selCustomizationType;
OP需要理解ORDINAL
的含义。从另一个角度看,你的答案看起来不错,GetValus是如何在GetName@NoviceToDotNet更新了我的回答它说一些无效参数不正确wirkit说“System.Enum.Parse(System.Type,string)”的最佳重载方法匹配'has some invalid arguments it say some invalid arguments not wirkit say'System.Enum.Parse(System.Type,string)'的最佳重载方法匹配具有一些无效参数