在C#中控制ListBox某一行的字体颜色

private void Form1_Load(object sender, EventArgs e)
{
listBox1.Items.Add(
"红色");
listBox1.Items.Add(
"黄色");
listBox1.Items.Add(
"蓝色");
listBox1.DrawMode
= DrawMode.OwnerDrawFixed; // 属性里设置
}

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
Color vColor
= e.ForeColor;
switch (e.Index)
{
case 0: vColor = Color.Red; break;
case 1: vColor = Color.Yellow; break;
case 2: vColor = Color.Blue; break;
}
e.Graphics.FillRectangle(
new SolidBrush(vColor), e.Bounds);
e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(), e.Font,
new SolidBrush(e.ForeColor), e.Bounds);
e.DrawFocusRectangle();
}

你可能感兴趣的:(listbox)