我想将 ComboBox 的第一项设置为红色,并使用此代码执行此任务:
private void cbTreeViewFolder_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
if (e.Index >= 0)
{
string text;
Brush brush;
MyClass file = ((MyClass)((ComboBox)sender).Items[e.Index]);
if (e.Index == 0)
{
text = file.Path;
brush = Brushes.Red;
}
else
{
text = file.Nome;
brush = Brushes.Black;
}
e.Graphics.DrawString(text, ((Control)sender).Font, brush, e.Bounds.X, e.Bounds.Y);
}
}
它一直有效,直到我清除 ComboBox,因为在这种情况下我不知道如何管理它,并且所有项目都保留在 ComboBox 中。有人知道问题的解决方案吗?