相关疑难解决方法(0)

如何将枚举绑定到组合框

我会使用组合框控件绑定枚举的值.

我写了这段代码:

cboPriorLogicalOperator.DataSource = Enum.GetValues(typeof(MyEnum))
    .Cast<MyEnum>()
    .Select(p => new { Key = (int)p, Value = p.ToString() })
    .ToList();

myComboBox.DisplayMember = "Value";
myComboBox.ValueMember = "Key";
Run Code Online (Sandbox Code Playgroud)

它运作良好,但我想知道是否有一个更简单的方法.

c# winforms

39
推荐指数
2
解决办法
2万
查看次数

如何在组合框中显示枚举值?

如何在组合框中显示枚举值?下面的代码导致组合框的所有显示名称都是"caseHandler.cState".我希望它具有枚举值的实际名称.

我的枚举定义如下:

public enum caseState
{
    Active = 1,
    Finished,
    Problem
}
Run Code Online (Sandbox Code Playgroud)

我有一个定义为这样的类:

public class cState
{    
    public string _name;
    public int _id;

    public cState(int id,string name)
    {
        _name = name;
        _id = id;
    }
}
Run Code Online (Sandbox Code Playgroud)

填充我的组合框的代码:

ArrayList AL = new ArrayList();

foreach (string cs in Enum.GetNames(typeof(caseState)))
{
    cState aEnum = new cState((int)Enum.Parse(typeof(caseState),cs),cs);
    AL.Add(aEnum);
}


cbState.DisplayMember = "_name";
cbState.ValueMember = "_id";

cbState.DataSource = AL;
Run Code Online (Sandbox Code Playgroud)

c# enums combobox winforms

13
推荐指数
2
解决办法
2万
查看次数

Winforms将Enum绑定到单选按钮

如果我有三个单选按钮,将它们绑定到具有相同选择的枚举的最佳方法是什么?例如

[] Choice 1
[] Choice 2
[] Choice 3

public enum MyChoices
{
    Choice1,
    Choice2,
    Choice3
}
Run Code Online (Sandbox Code Playgroud)

c# data-binding enums radio-button winforms

7
推荐指数
1
解决办法
7186
查看次数

将枚举值添加到简单的组合框中

我有一个关于C#和WPF的简单问题.我的问题将在我的尝试后继续:

private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (var item in Races)
            {
                cbRace.Items.Add(item);
            }
        }
    }

    enum Races
    {
        Human=1,
        Dwarf,
        Elf,
        Orc,
        Goblin,
        Vampire,
        Centaur
    }
Run Code Online (Sandbox Code Playgroud)

好吧,我的问题是如何将值(例如Human,dwarf,elf ....)添加到组合框中:cbRace?抱歉,我是C#的新手,所以如果有人可以帮助我,我会集会赞赏:),提前谢谢.

c# wpf enums combobox

7
推荐指数
3
解决办法
4万
查看次数

验证它的 SelectedText 属性是否为空的组合框总是失败

简单的问题:我正在检查组合框是否已使用string.IsNullOrEmpty(). 问题是,即使选择了 ,也会出现错误消息。我究竟做错了什么?

这是我的代码:

private void button1Click(object sender, EventArgs e)
{
    if (string.IsNullOrEmpty(comboBox1.SelectedText))//here should skip to else - but doesn't
    {
        MessageBox.Show("You must select a conversion type", "Error");
    }
    else
    {
        if (comboBox1.SelectedText == "Currency")
        {
            double input = Convert.ToDouble(textBox1.Text);
            if (!string.IsNullOrEmpty(comboBox2.SelectedText))
            {
                string type = comboBox2.SelectedText;
                double result = convertCurrency(type, input);
                if (result != -1)
                {
                    label1.Text = Convert.ToString(result);
                }
             }
             else
             {
                 MessageBox.Show("You must select a conversion type", "Error");
             }
         }
         else
         {
             MessageBox.Show("curency");
         }
     } …
Run Code Online (Sandbox Code Playgroud)

c# visual-studio-2010

3
推荐指数
1
解决办法
2万
查看次数