One word in bold inside a winform control

I need a way to make the highlighted word bold (the first two characters of each item in the list), and nothing more, something like this:

01 car
02 house
03 market

As for these three, being elements in the list control, ALWAYS the first two characters, the rest should not be bold.

Is there any practical way to do this?

Data:

  • Visual studio 2008
  • .NET 3.5

request:

private void lstMaster_DrawItem(object sender, DrawItemEventArgs e) { //TEST e.DrawBackground(); Brush myBrush = Brushes.Black; Pen pen = new Pen(myBrush); e.Graphics.DrawRectangle(pen, 0, 0, 10, 10); //BREAKPOINT HERE e.Graphics.DrawString("aaa" + lstMaster.Items[e.Index].ToString(), e.Font, myBrush, e.Bounds, StringFormat.GenericDefault); e.DrawFocusRectangle(); } 

It’s just keept the same, no rectangle, no β€œAAA”, no square breakpoint reached ...

+6
c # formatting winforms listbox
source share
2 answers

You may need to use the DrawItem event of the list and from there you can draw your objects, but you would like to:

MSI DrawItem Event

Here is an example that draws each element in a different color:

 private void ListBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { // Draw the background of the ListBox control for each item. e.DrawBackground(); // Define the default color of the brush as black. Brush myBrush = Brushes.Black; // Determine the color of the brush to draw each item based // on the index of the item to draw. switch (e.Index) { case 0: myBrush = Brushes.Red; break; case 1: myBrush = Brushes.Orange; break; case 2: myBrush = Brushes.Purple; break; } // Draw the current item text based on the current Font // and the custom brush settings. e.Graphics.DrawString(ListBox1.Items[e.Index].ToString(), e.Font, myBrush, e.Bounds, StringFormat.GenericDefault); // If the ListBox has focus, draw a focus rectangle around the selected item. e.DrawFocusRectangle(); } 

According to the documentation, you will also need to change the DrawMode property in the list to trigger the event:

This event is used by the owner-created ListBox. An event occurs only when the DrawMode property is set to DrawMode.OwnerDrawFixed or DrawMode.OwnerDrawVariable. You can use this event to perform the tasks necessary to draw items in a ListBox.

+6
source share

I think you can use HTML markup from VS 2008, for example. <b>01</b> car for 01 car, for some text. I have to check if this applies to the list.

0
source share

All Articles