0

我想将 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 中。有人知道问题的解决方案吗?

4

1 回答 1

0
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{    
     if (((ComboBox)sender).Items.Count != 0) {
     e.DrawBackground();        
     string text = ((ComboBox)sender).Items[e.Index].ToString();
     Brush brush;         
     if (e.Index == 0)
        brush = Brushes.Red;
     else
        brush = Brushes.Black;
     e.Graphics.DrawString(text, ((Control)sender).Font, brush, e.Bounds.X, e.Bounds.Y);
     }
}
于 2013-01-04T10:09:28.833 回答