现在的位置: 首页 > 综合 > 正文

改变ListBox中项的字体颜色。

2013年10月20日 ⁄ 综合 ⁄ 共 851字 ⁄ 字号 评论关闭

比如要在ListBox中显示:

火树银花
雪山飞狐
海河之滨
北京古都
万水千山

代码实现:

       private List<Color> colorList;

        
public Form1()
        
{
            InitializeComponent();
            colorList 
= new List<Color>();
            colorList.AddRange(
new Color[] { Color.Red, Color.Blue, Color.Yellow, Color.Violet, Color.Thistle });
            
//listBox1.Items.AddRange(new object[] { Color.Red, Color.Blue, Color.Yellow, Color.Violet, Color.Thistle });
            listBox1.DrawMode = DrawMode.OwnerDrawFixed;
            listBox1.DrawItem 
+= new DrawItemEventHandler(listBox1_DrawItem);
        }


        
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        
{
            e.DrawBackground();
            e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, 
new SolidBrush(colorList[e.Index]), e.Bounds);
            e.DrawFocusRectangle();
        }

抱歉!评论已关闭.