Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

  • Leaders
Posted (edited)

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.


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;
};
};
[/Code]

 

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));
}
[/Code]

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));
[/Code]

If it looks weird, that's because my Windows is skinned.

Edited by snarfblam
[sIGPIC]e[/sIGPIC]
Posted

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

  • Leaders
Posted

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.

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...