我有一个场景,我用模板名称填充组合框。在这些模板中,有一个是默认模板。我想在填充组合框时突出显示默认模板名称(以便用户知道其中哪一项是默认的)。可以这样做吗?如果是的话怎么办?我正在使用 C# 2.0 中的 Windows 窗体。
这在一定程度上取决于您想要如何突出显示该项目。如果您想以粗体呈现默认项目的文本,您可以像这样实现(为此,您需要设置DrawMode
组合框的OwnerDrawFixed
,当然还有将 DrawItem 事件连接到事件处理程序):
我已经用模板对象填充了组合框,定义如下:
private class Template
{
public string Name { get; set; }
public bool IsDefault { get; set; }
public override string ToString()
{
return this.Name;
}
}
...DrawItem 事件的实现如下:
private void ComboBox_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index < 0)
{
return;
}
Template template = comboBox1.Items[e.Index] as Template;
if (template != null)
{
Font font = comboBox1.Font;
Brush backgroundColor;
Brush textColor;
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
backgroundColor = SystemBrushes.Highlight;
textColor = SystemBrushes.HighlightText;
}
else
{
backgroundColor = SystemBrushes.Window;
textColor = SystemBrushes.WindowText;
}
if (template.IsDefault)
{
font = new Font(font, FontStyle.Bold);
}
e.Graphics.FillRectangle(backgroundColor, e.Bounds);
e.Graphics.DrawString(template.Name, font, textColor, e.Bounds);
}
}
我希望这应该能让你朝着正确的方向前进。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)