【问题标题】:WinForms Custom ComboBox does not display names of List<> itemsWinForms 自定义组合框不显示 List<> 项目的名称
【发布时间】:2020-07-10 21:34:18
【问题描述】:

我正在尝试在 c# Winforms 中创建一个自定义 ComboBox,以便与自定义突出显示颜色一起使用。 从互联网上找到了一些示例并进行了一些修改以保持我的代码干净。但是有一些错误我无法修复,所以我需要您的帮助。

这里是我的自定义 ComboBox 类:

using System.Drawing;
using System.Windows.Forms;

namespace TesTApp
{
    class ThemedComboBox : ComboBox
    {
        protected Color _HighLightColor;       
        [System.ComponentModel.Description("High Light Color for selection"),
        System.ComponentModel.Category("HighLight"),
        System.ComponentModel.DefaultValue(typeof(Color), "Gray")]
        public Color HighlightColor
        {
            get { return _HighLightColor; }
            set
            {
                _HighLightColor = value;
                Invalidate();
                Update();
            }
        }

        public ThemedComboBox()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.DrawMode = DrawMode.OwnerDrawFixed;
            HighlightColor = Color.FromArgb(255, 167, 36);
            this.DrawItem += new DrawItemEventHandler(ComboBox_DrawItem);            
        }
        void ComboBox_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index < 0)
                return;

            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                e.Graphics.FillRectangle(new SolidBrush(HighlightColor),
                                         e.Bounds);
            else
                e.Graphics.FillRectangle(new SolidBrush(BackColor),
                                         e.Bounds);

            e.Graphics.DrawString(Items[e.Index].ToString(), e.Font,
                                  new SolidBrush(ForeColor),
                                  new Point(e.Bounds.X, e.Bounds.Y));

            e.DrawFocusRectangle();
        }
    }
}

当我调用 List 填充 ComboBox 的方法时,DropDown 不显示项目的名称。

一开始我创建了一个类来填充数据

  public class testItems
    {
        public string itemName { get; set; }
        public int itemValue { get; set; }
    }

然后我调用填充 ComboBox 的方法:

int maxItem = 5;
private void TestComboBox1()
{
   List<testItems> _list = new List<testItems>();
   for (int i = 0; i < maxItem; i++)
   {
     _list.Add(new testItems
      {
        itemName = "Item no: " + i.ToString(),
        itemValue = i
       });
   }
   themedComboBox1.DataSource = _list;
   themedComboBox1.DisplayMember = "itemName";
   themedComboBox1.ValueMember = "itemValue";
}

使用此方法,ComboBox DropDown 不会显示项目的名称(DisplayMember)。

然后我尝试用字符串数组填充 ComboBox。使用字符串数组 ComboBox 显示项目,但这样我无法为项目赋值。

这是我用字符串数组填充 ComboBox 的方法:

 int maxItem = 5;
 private void TestComboBox2()
 {
   string[] items = new string[maxItem];
   for (int i = 0; i < maxItem; i++)
   {
      items[i] = "Item No : " + i;
   }
   themedComboBox2.DataSource = items;
 }

我只想拥有一个带有自定义突出显示颜色的自定义组合框。我在哪里做错了?你能帮我纠正一下吗?

【问题讨论】:

  • What causes ComboBox to show System.Data.DataRowView。 -- 您还可以将当前的Item 转换为基础对象类型。顺便说一句,设置DisplayMember 之前你设置DataSource
  • 同上:ListBox items do not show in OwnerDrawFixed Mode,代码更完整。
  • DataSource 属性之前设置DisplayMember 属性与您当前的问题无关,这是其他问题(如果列表数量增加,您需要考虑这一点。不仅与组合框控件)。与您的问题 相关的是我发布的两个链接。如果您真的想正确使用ValueMemberDisplayMember,请看一下(这样您就不需要一直强制转换以从底层对象中提取值)并了解问题出在哪里。跨度>
  • 是的,这是您需要的部分之一。您的 ComboBox 需要使用所有类型的数据源。如果您想使用 DataTable 或 Dictionary 作为 DataSource 怎么办?您是否覆盖 DataRowView 对象的 ToString()?还是字典的KeyValuePair?关于DisplayMember:当你设置这个属性时,如果已经设置了一个,(any) Control需要重新绑定DataSource,结果是查询了两次数据源。如果您的数据源来自 Web 或远程数据服务器,这很烦人。如果您有一个包含 5 个元素的本地列表,您将不会注意到它。
  • 是的,没错。正确设置您的 ComboBox(或 ListBox 或其他),如果/当 DataSource 类型更改时,您无需更改一行代码。

标签: c# winforms combobox


【解决方案1】:

您看到的“TestTApp.testItems”值实际上是在虚拟方法Object.ToString() 中生成的。如果您唯一的问题是替换这些值,您可以为 testItems 类覆盖 ToString 方法(顺便说一下,我建议将其命名为 TestItem

看这个:

public class testItems
{
    public string itemName { get; set; }
    public int itemValue { get; set; }

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

使用列表和组合框(不限于组合框)相当简单,您不必像处理一般数据源那样担心行号和列名。其实你甚至不需要设置DisplayMemberDataMember

请注意,在使用这种方法时,您使用的是 testItems 类型的值,而不是您希望 ValueMember 持有的类型。例如,调用themedComboBox1.SelectedValue 返回一个testItems 对象(而不是int)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多