1

我有一个 League.cs 文件,其中包含一个项目的所有联赛

当我尝试在组合框中显示此数据时,它显示“App.Toolkit.Parameter”而不是联赛名称。

我将此代码用于组合:

foreach (League item in League.GetAll()) { comboBox1.Items.Add(item); }

和 League.cs 文件:

using System.Collections.Generic;

namespace App.Toolkit.Parameter
{
    public class League : SearchParameterBase<uint>
    {
        public const uint BarclaysPremierLeague = 13;

        public const uint Bundesliga = 19;

        public const uint LigaBbva = 53;

        public const uint Ligue1 = 16;

        public const uint SerieA = 31;

        private League(string description, uint value)
        {
            Description = description;
            Value = value;
        }

        public static IEnumerable<League> GetAll()
        {
            yield return new League("Barclays Premier League", BarclaysPremierLeague);
            yield return new League("Bundesliga", Bundesliga);
            yield return new League("Liga BBVA", LigaBbva);
            yield return new League("Ligue 1", Ligue1);
            yield return new League("Serie A", SerieA);
        }
    }
}

感谢广告 :)

4

5 回答 5

3

您可以设置DisplayMemberComboBox 的:

comboBox1.DisplayMember = "Description";

或者你可以ToString在你的League类中覆盖:

public override string ToString()
{
 return Description;
}
于 2013-05-30T12:03:37.970 回答
2

你应该设置

comboBox1.DisplayMember = "Description";
comboBox1.ValueMember = "Value";

也就是说,我建议使用数据绑定;

var bs = new BindingSource(League.GetAll().ToList(), null);

comboBox1.DisplayMember = "Description";
comboBox1.ValueMember = "Value";
comboBox1.DataSource = bs;

这让你

a) 使用 b) 访问选定的 leage 使用bs.Current
c) 更改选定bs.MoveNext() / bs.MoveFirst()
的 Leage 而不修改组合框,只需将 bs.DatasSource 设置为其他值。

更少的代码,更少的错误,更少的头痛

private void Init()
{
    var bs = new BindingSource(...);
    bs.AddingNew += new AddingNewEventHandler(bs_AddingNew);
}

void bs_AddingNew(object sender, AddingNewEventArgs e)
{
    string name = AskForName();
    e.NewObject = CreateLeage(name);
}

现在你可以打电话

bs.AddNew();

在您的代码中的某处。

于 2013-05-30T12:03:35.643 回答
1

您可以覆盖联赛类中的 ToString 方法:

  public override string ToString()
    {
        return this.description;
    }
于 2013-05-30T12:08:04.670 回答
0

这将有助于覆盖该ToString()方法。

于 2013-05-30T12:02:27.453 回答
0

您将对象添加到组合框并需要更改代码:

using System.Collections.Generic;

namespace App.Toolkit.Parameter
{
public class League : SearchParameterBase<uint>
{
    public const uint BarclaysPremierLeague = 13;

    public const uint Bundesliga = 19;

    public const uint LigaBbva = 53;

    public const uint Ligue1 = 16;

    public const uint SerieA = 31;

    private League(string description, uint value)
    {
        Description = description;
        Value = value;
    }



    public static IEnumerable<League> GetAll()
    {
        yield return new League("Barclays Premier League", BarclaysPremierLeague);
        yield return new League("Bundesliga", Bundesliga);
        yield return new League("Liga BBVA", LigaBbva);
        yield return new League("Ligue 1", Ligue1);
        yield return new League("Serie A", SerieA);
    }

    public override string ToString()
    {
        return Description;
    }
  }
}    

ovveride toString() 方法。

于 2013-05-30T12:11:33.247 回答