Vincent Dagpin
Vincent Dagpin

Reputation: 3611

ListBox AllowHTML in c#

is there a way to display different TextFormat/Style per item in ListBox

Example:

Data1: value
Data2: value world
Data3: value hello

i tried this but i cant make the next string in bold

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();
    }

SOLUTION

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)
        {

        }
    }

Upvotes: 0

Views: 117

Answers (2)

Hans Passant
Hans Passant

Reputation: 942040

You need to draw two strings, the second one using a bold font. A full example:

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();
    }
}

Produces:

enter image description here

Upvotes: 1

Amit
Amit

Reputation: 22086

You can do it as mention in this Link. I know you are trying it differently, but it can help you.

There is a other link as well. This link describe how you can display items in a ListBox with different font style.

Here is other good link as well.

Upvotes: 0

Related Questions