Ras*_*dit 2 c# .net-2.0 winforms
我有一个场景,我正在使用模板名称填充组合框.在模板中,一个是默认模板.我想在填充组合框时突出显示默认模板名称(以便用户知道这些项中的哪一个是默认值).有可能这样做吗?如果有,怎么样?我在C#2.0中使用Windows窗体.
这取决于你想如何高亮显示项目.如果你想以粗体呈现默认项目的文本,你可以像这样实现(为此你需要设置DrawModeComboBox OwnerDrawFixed,当然,将DrawItem事件连接到事件处理程序):
我用Template对象填充了组合框,定义如下:
private class Template
{
public string Name { get; set; }
public bool IsDefault { get; set; }
public override string ToString()
{
return this.Name;
}
}
Run Code Online (Sandbox Code Playgroud)
...并且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);
}
}
Run Code Online (Sandbox Code Playgroud)
我希望,这应该让你朝着正确的方向前进.
| 归档时间: |
|
| 查看次数: |
5917 次 |
| 最近记录: |