Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7853421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:41:08+00:00 2026-06-02T19:41:08+00:00

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

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

        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T19:41:11+00:00Added an answer on June 2, 2026 at 7:41 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Rails, is there a way to display different error messages on a production
Is there a way to display flash applications using Gnash renderer (I'm not averse
is there a way to display the animated spinning wheel while the main thread
Is there a way to display and edit values in the PropertyGrid (and his
Is there a way to display an action-specific authorisation message for when an [Authorize]
Is there a way to display the dropdown menu shown on sitepoint by mouse
Is there a way to display an image in the reminder notification page for
I just wonder there a way to display the files in document directory with
When viewing submitted changelists in p4v, is there a way to display a particular
Is there any way to output/display information from a MATLAB program without an ending

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.