WinForms Custom ComboBox 不显示 List<> 项的名称
WinForms Custom ComboBox does not display names of List<> items
我正在尝试在 c# Winforms 中创建自定义 ComboBox 以使用自定义突出显示颜色。
从互联网上找到了一些例子并做了一些修改以保留我的代码 clean.But 有一些错误我无法修复所以我需要你的帮助。
这里是我的自定义组合框 class:
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 不显示项目的名称。
起初我创建了一个class来填充数据
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 array.With 字符串数组 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;
}
我只想拥有一个带有自定义高亮显示的自定义组合框 color.Where 我做错了吗?你能帮我更正一下吗?
你看到的值"TestTApp.testItems"实际上是在虚方法Object.ToString()
中生成的。如果您唯一的问题是替换这些值,您可以为 testItems
class 重写 ToString 方法(顺便说一句,我建议命名为 TestItem
)
看到这个:
public class testItems
{
public string itemName { get; set; }
public int itemValue { get; set; }
public override string ToString()
{
return itemName;
}
}
使用列表和组合框(不限于组合框)非常简单,您不必像处理一般数据源那样担心行号和列名。事实上,你甚至不需要设置 DisplayMember
和 DataMember
.
请注意,在使用这种方法时,您使用的是 testItems
类型的值,而不是您希望 ValueMember
保留的类型。例如,调用 themedComboBox1.SelectedValue
returns 一个 testItems
对象(而不是 int
)。
我正在尝试在 c# Winforms 中创建自定义 ComboBox 以使用自定义突出显示颜色。 从互联网上找到了一些例子并做了一些修改以保留我的代码 clean.But 有一些错误我无法修复所以我需要你的帮助。
这里是我的自定义组合框 class:
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 不显示项目的名称。
起初我创建了一个class来填充数据
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 array.With 字符串数组 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;
}
我只想拥有一个带有自定义高亮显示的自定义组合框 color.Where 我做错了吗?你能帮我更正一下吗?
你看到的值"TestTApp.testItems"实际上是在虚方法Object.ToString()
中生成的。如果您唯一的问题是替换这些值,您可以为 testItems
class 重写 ToString 方法(顺便说一句,我建议命名为 TestItem
)
看到这个:
public class testItems
{
public string itemName { get; set; }
public int itemValue { get; set; }
public override string ToString()
{
return itemName;
}
}
使用列表和组合框(不限于组合框)非常简单,您不必像处理一般数据源那样担心行号和列名。事实上,你甚至不需要设置 DisplayMember
和 DataMember
.
请注意,在使用这种方法时,您使用的是 testItems
类型的值,而不是您希望 ValueMember
保留的类型。例如,调用 themedComboBox1.SelectedValue
returns 一个 testItems
对象(而不是 int
)。