在C#代码中设置一个列表框项目的字体和颜色 [英] Set the font and color of a listbox item by code in C#

查看:795
本文介绍了在C#代码中设置一个列表框项目的字体和颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我忙,我在C#中的寄存器读取器使用自定义列表框。现在,我想在一个确定的项目来设置一个确定的项目使用不同的字体和颜色比其他人。我检查这个问题和答案我做下面的代码:

I'm busy with a customized list box that I use as a register reader in c#. Now I want to set a determined item in a determined item with a different font and color than the rest. I checked This question and from the answers I made the following code:

private void myListBox_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
    e.DrawBackground();
    Font myFont;
    Brush myBrush;
    int i = e.Index;

    if (e.Index == 3)
    {
        myFont = e.Font;
        myBrush = Brushes.Black;
    }
    else
    {
        myFont = new Font("Microsoft Sans Serif", 9.75f, FontStyle.Bold);
        myBrush = Brushes.CadetBlue;
    }

    e.Graphics.DrawString(myListBox.Items[i].ToString(), myFont, myBrush, e.Bounds, StringFormat.GenericDefault);
}

和使用

this.myListBox.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.myListBox_DrawItem);



呼叫亦对此不抛出异常,但我看到我要处理的线路没有变化。有我丢失的东西?

The call does not throw an exception whatsoever, but I see no change on the line I want to handle. Is there something I am missing?

推荐答案

您缺少的下一行的 IntializeComponent()补充一点:

You are missing one more line in your IntializeComponent() add this:

this.myListBox.DrawMode = DrawMode.OwnerDrawFixed;

在附加的事件。

这篇关于在C#代码中设置一个列表框项目的字体和颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆