views:

1662

answers:

1

In Visual c# Express Edition, is it possible to make some (but not all) items in a ListBox bold? I can't find any sort of option for this in the API.

+6  A: 

You need to change listbox's DrawMode to DrawMode.OwnerDrawFixed. Check out these articles on msdn:
DrawMode Enumeration
ListBox.DrawItem Event
Graphics.DrawString Method

Also look at this question on msdn forums:
Question on ListBox items

A simple example (both items - Black-Arial-10-Bold):

 public partial class Form1 : Form
 {
    public Form1()
    {
        InitializeComponent();

        ListBox1.Items.AddRange(new Object[] { "First Item", "Second Item"});
        ListBox1.DrawMode = DrawMode.OwnerDrawFixed;
    }

    private void ListBox1_DrawItem(object sender, DrawItemEventArgs e)
    {
        e.DrawBackground();
        e.Graphics.DrawString(ListBox1.Items[e.Index].ToString(), new Font("Arial", 10, FontStyle.Bold), Brushes.Black, e.Bounds);
        e.DrawFocusRectangle();
    }
 }
Mindaugas Mozūras
Great example and links. Wish I could vote this up twice!
magnifico