0

有没有办法在 ListBox 中为每个项目显示不同的 TextFormat/Style

例子:

Data1:价值
Data2:价值世界
Data3:价值你好

我试过了,但我不能用粗体显示下一个字符串

private void lbDetails_DrawItem(object sender, DrawItemEventArgs e)
    {
        e.DrawBackground();

        Rectangle rec = e.Bounds;

        string[] temp = lbDetails.Items[e.Index].ToString().Split(':');

        e.Graphics.DrawString(temp[0], 
            new Font("Arial",8, FontStyle.Regular),
            Brushes.Black, 
            rec, 
            StringFormat.GenericDefault);

        e.DrawFocusRectangle();
    }

解决方案

private void lbDetails_DrawItem(object sender, DrawItemEventArgs e)
    {
        try
        {
            e.DrawBackground();

            string[] temp = lbDetails.Items[e.Index].ToString().Split(':');

            SizeF prevStr = e.Graphics.MeasureString(temp[0] + ": ", new Font("Arial", 8, FontStyle.Regular));
            SizeF nextStr = e.Graphics.MeasureString(temp[1], new Font("Arial", 8, FontStyle.Bold));

            RectangleF firstRec = new RectangleF(e.Bounds.X, e.Bounds.Y, prevStr.Width, prevStr.Height);
            RectangleF secondRec = new RectangleF(prevStr.Width, e.Bounds.Y, nextStr.Width, nextStr.Height);

            e.Graphics.DrawString(temp[0] + ": ",
                new Font("Arial", 8, FontStyle.Regular),
                Brushes.Black,
                firstRec,
                StringFormat.GenericDefault);

            e.Graphics.DrawString(temp[1],
                new Font("Arial", 8, FontStyle.Bold),
                Brushes.Black,
                secondRec,
                StringFormat.GenericDefault);

            //e.Graphics.DrawRectangle(Pens.Red, firstRec.X, firstRec.Y, firstRec.Width, firstRec.Height);

            e.DrawFocusRectangle();
        }
        catch (Exception ex)
        {

        }
    }
4

2 回答 2

1

您需要绘制两个字符串,第二个使用粗体字。一个完整的例子:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        listBox1.DrawMode = DrawMode.OwnerDrawFixed;
        listBox1.DrawItem += new DrawItemEventHandler(listBox1_DrawItem);
        listBox1.Items.AddRange(new object[] { "Data1: value", "Data2: hello world", "Data3: hello hello"});
    }

    void listBox1_DrawItem(object sender, DrawItemEventArgs e) {
        var box = (ListBox)sender;
        e.DrawBackground();
        if (e.Index < 0) return;
        string[] temp = box.Items[e.Index].ToString().Split(':');
        int size = (int)(TextRenderer.MeasureText(e.Graphics, temp[0], box.Font).Width + 0.5);
        var rc = new Rectangle(e.Bounds.Left, e.Bounds.Top, (int)size, e.Bounds.Height);
        var fmt = TextFormatFlags.Left;
        TextRenderer.DrawText(e.Graphics, temp[0] + ":", box.Font, rc, e.ForeColor, fmt);
        if (temp.Length > 1) {
            using (var bold = new Font(box.Font, FontStyle.Bold)) {
                rc = new Rectangle(e.Bounds.Left + size, e.Bounds.Top, e.Bounds.Width - size, e.Bounds.Height);
                TextRenderer.DrawText(e.Graphics, temp[1], bold, rc, e.ForeColor, fmt);
            }
        }
        e.DrawFocusRectangle();
    }
}

产生:

在此处输入图像描述

于 2012-04-28T20:58:46.290 回答
0

您可以按照此链接中的说明进行操作。我知道你正在尝试不同的方法,但它可以帮助你。

还有另一个链接。此链接描述了如何在具有不同字体样式的 ListBox 中显示项目。

这里还有其他很好的链接。

于 2012-04-28T20:36:13.923 回答