views:

307

answers:

3

I want some way to make a given word bold(the first two characters of each item in a listbox), and nothing else, something like this:

01 car
02 house
03 market

As for these three, being items in a listbox control, ALWAYS the first two characters, the rest should not be bold.

Is there any practical way for doing so ?

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 just keept the same, no rectangle, no "AAA", or square no breakpoint reached...

A: 

I think that you can use HTML markup from VS 2008 onwards e.g. <b>01</b> car for 01 car, for some text. I'd have to check whether this applies to listboxes.

ChrisBD
just checked.. doesent =(
MarceloRamires
+5  A: 

It is possible, You will need to use the listbox's DrawItem event and from there you can draw your items however you would like to:

DrawItem event on MSDN

Here is an example that draws each item with 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 of the listbox in order the fire the event:

This event is used by an owner-drawn ListBox. The event is only raised when the DrawMode property is set to DrawMode.OwnerDrawFixed or DrawMode.OwnerDrawVariable. You can use this event to perform the tasks needed to draw items in the ListBox.

gyurisc
Is this as efficient? (talking about resources consumption and processing usage) Any functionality loss? like selecting, copying, being able to gather the string back or anything?
MarceloRamires
I think it is efficient of course it is possible to screw this up, if for example somehow you trigger a redraw from this code, then it will start flashing or you do something computing intensive in this function
gyurisc
As far as I understand that this event is only called for redrawing the individual item, so it will only run for visible items. this is again very good and efficient and you do not have to take care of this from your code.
gyurisc
I've done it, to be sure, created the event in the event tab clicking on "drawicon" textbox, then wrote the code, but it seems like it uses an old one, a breakpoint inside it wasn't reached.. what else do i have to do to assign this function to the listbox ?
MarceloRamires
Can you please send me your code? or post it here? I would need to look at it and debug it
gyurisc
Add a listbox to your form and make sure that you are clicking on the event handler of the listbox
gyurisc
Added to the question.
MarceloRamires
Checked the documentation and it seems that you need to change the DrawMode to OwnerDrawn.
gyurisc
A: 

There is a 3-rd party control, IntegralUI ListBox, where you can set each word or character in different colors. It is very easy, just create an item with this code:

item.Content = "<div><b>01</b> car</div>";

To set in different colors just use this code:

item.Content = "<div><b style=\"textcolor:Red\">01</b> car</div>";
Lokey