ListBox with different fonts

guilt92

Newcomer
Joined
Feb 9, 2006
Messages
2
Hello,

I am programming in C++.Net and really don' find how i could create a listbox with different color for each item, Example :

Item1
Item2
Item3

I saw there were already a solution to this problem in this forum but the file attached doesn't work so if someone can help it would be very nice

Thank you
 
I recommending researching owner drawn ListBoxes. The jist of it is this:
-Set DrawMode to OwnerDrawnFixed if all the items will be the same size. This probably suits you. If not, use OwnerDrawnVariable
-Attach a delegate to the DrawItem event. If DrawMode is OwnerDrawnFixed, set the size by the ListBox.ItemHeight property. If DrawMode is OwnerDrawnVariable, attach a delegate to the MeasureItem event.
-The event handler functions will be passed the object to measure/draw.
-It may be a good idea to create your own class to hold information relevant to rendering (colors, images, etc.)

Sorry if my C++ looks like garbage; clr/c++ is actually entirely new to me today. This is a class (inline in the header) I wrote to hold text and color data. ToString should be overridden because it's the function called to get the string that will be displayed in a normal ListBox.
Code:
using namespace System;
using namespace System::Drawing;

public ref class MyListItem: System::Object
{
public:

	String^ Text;
	Color Clr;

	MyListItem(String ^DisplayText, Color c): Text(DisplayText), Clr(c){
	}

	virtual String^ ToString() override {
		return Text;
	};
};

This is generally what your event handlers should look like.
Code:
// I used DrawMode = OwnerDrawnVariable
// This function will be called to find out how big you want your items to be
System::Void listBox1_MeasureItem(System::Object^  sender, System::Windows::Forms::MeasureItemEventArgs^  e) {
	e->ItemHeight = 16; // Change height to suit taste
	// Default width (100% of listbox).
 }
 
//This function will be called for you to render each item
System::Void listBox1_DrawItem(System::Object^  sender, System::Windows::Forms::DrawItemEventArgs^  e) {
	// -1 indicates no item
	if(e->Index == -1) return;
	
	// Get stongly typed handle to item
	MyListItem^ Item = ((MyListItem^)(listBox1->Items[e->Index]));
	
	// Render text
	e->Graphics->DrawString(Item->ToString(),
		this->Font, 
		gcnew SolidBrush(Item->Clr),
		(float)(e->Bounds.X + 8),
		(float)(e->Bounds.Y + 3));
	}
I added objects to the list using this code:
Code:
this->listBox1->Items->Add(gcnew MyListItem("Red", Color::Green));
this->listBox1->Items->Add(gcnew MyListItem("Green", Color::Blue));
this->listBox1->Items->Add(gcnew MyListItem("Blue", Color::Red));
If it looks weird, that's because my Windows is skinned.
 

Attachments

Last edited:
Thanks, this really help, but i have some troubles creating the same program :

i m using visual studio 2003 and i can't find where i should create the class MyListItem, i have tried in a new .h but i get some errors.

Would you mind sending me your files or explaining a little more pliz ?

Thanks a lot
 
Ouch! I'm sorry, but if you are using C++ 2003, then you are using Managed Extensions, whereas C++ 2005 uses a C++/CLR syntax. C++ 2003 won't compile the code I gave you.

MyListItem should go in its own header file. Maybe you can find someone to translate the code, because I don't know managed extensions.
 
Back
Top