Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make some items in a ListBox bold?

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.

like image 487
Martin Doms Avatar asked Nov 29 '08 07:11

Martin Doms


People also ask

How do I change the font in ListBox?

Right-click the multicolumn listbox and select Create»Property Node»Active Row»Cell Font»Size from the shortcut menu to create an Active Row Fonte Size Property. Right-click the Active Row Fonte Size Property and select Change To Write from the shortcut menu. You now can specify the font size for the active rows.

How do I edit items in ListBox?

To edit an item in a listbox, all you need is a simple editbox that overlays on a listbox item. The idea is to create a TextBox control and keep it hidden and show it whenever required. Two events are added to the EditBox. KeyPress event to check if the enter key is pressed when the user has finished editing the item.

How do I display items in ListBox?

Step 1: Create a list box using the ListBox() constructor is provided by the ListBox class. // Creating ListBox using ListBox class constructor ListBox mylist = new ListBox(); Step 2: After creating ListBox, set the Items property of the ListBox provided by the ListBox class.

Which property is used for displaying the selected items of ListBox?

To retrieve a collection containing all selected items in a multiple-selection ListBox, use the SelectedItems property. If you want to obtain the index position of the currently selected item in the ListBox, use the SelectedIndex property.


1 Answers

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();
    }
 }
like image 123
Mindaugas Mozūras Avatar answered Sep 28 '22 07:09

Mindaugas Mozūras