0

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

        }
    }
4
  • Have you set a breakpoint in this code and that breakpoint has been hit? Commented Apr 28, 2012 at 20:25
  • yep.. it normally works.. like per item in listbox is getting bold.. my point is if the item is "Data3: value hello" <-- only the "value hello" will be in bold state.. Commented Apr 28, 2012 at 20:29
  • i think it will not gonna work then.. :) i will just find another solution.. Commented Apr 28, 2012 at 20:35
  • Graphics.MeasureString() gives the hint.. working it out.. ^_^ Thanks boss.. Commented Apr 28, 2012 at 20:53

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

this is just the same on my example.. :)
@vrynxzent: sorry for the redundancy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.