Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TListBox and colored items

7 views
Skip to first unread message

Kim Nurnberg

unread,
Mar 8, 1999, 3:00:00 AM3/8/99
to
How can I change the color of an item in a list box? After the item has

been selected or double clicked I would like to change the color of that

one item. Can someone please post a snip?
TIA,
Kim


Jody Hagins (TeamB)

unread,
Mar 9, 1999, 3:00:00 AM3/9/99
to


Please do not post the same question to multiple groups.

Happy Building!
Jody
http://www.hagins.com/

Damon Chandler

unread,
Mar 9, 1999, 3:00:00 AM3/9/99
to
First set your ListBox to OwnerDraw (fixed or variable). The goal is to
keep an array of flags that indicate that an item has been previously
selected. The every time the ListBox draws it's item, you'll check the
flag, and change the color accordingly. The OnDrawItem event can
sometimes be confusing. The thing to know is that it is called
repeatedly for every item in the ListBox, so when you redraw things,
there is no need for loops. The parameter Index is used to indicate
what item is currently being redrawn.

The following code will change a ListBox's item to red if
double-clicked...

//in header...
bool HasBeenSelected[1000];


//in source...
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
//Initialize our flags
for (int index = 0; index < 1000; index++) HasBeenSelected[index] =
false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox1DrawItem(TWinControl *Control, int
Index,
TRect &Rect, TOwnerDrawState State)
{
//Eliminate artifacts
ListBox1->Canvas->FillRect(Rect);

//Check to see if Index has been selected
if (HasBeenSelected[Index] == true)
{
//Color the text
ListBox1->Canvas->Font->Color = clRed;
}
//Check for selection rectangle
// and color font white so it's visible
else if (State.Contains(odSelected))
ListBox1->Canvas->Font->Color = clWhite;
else ListBox1->Canvas->Font->Color = clBlack;

ListBox1->Canvas->TextOut(Rect.Left + 1, Rect.Top,
ListBox1->Items->Strings[Index]);
}
//---------------------------------------------------------------------------


void __fastcall TForm1::ListBox1DblClick(TObject *Sender)
{
//Set the flag for double-clicked item to true
HasBeenSelected[ListBox1->ItemIndex] = true;
}


Damon Chandler
dm...@cornell.edu

Damon Chandler

unread,
Mar 9, 1999, 3:00:00 AM3/9/99
to
...draws its item...

J.A. Bijsterbosch

unread,
Mar 14, 1999, 3:00:00 AM3/14/99
to
Hello Damon,

Damon Chandler <dm...@cornell.edu> schreef in artikel
<36E56832...@cornell.edu>...
[ explanation snipped ]


> The following code will change a ListBox's item to red if
> double-clicked...
>
> //in header...
> bool HasBeenSelected[1000];
>
>
> //in source...
>
//--------------------------------------------------------------------------
-
> __fastcall TForm1::TForm1(TComponent* Owner)
> : TForm(Owner)
> {
> //Initialize our flags
> for (int index = 0; index < 1000; index++) HasBeenSelected[index] =
> false;
> }

Hmm, have you thought about to use the TListBox->Items->Objects[index]
property to set the selection flags? That way you can get rid of the
HasBeenSelected array...

Just my two cents...;-))

> Damon Chandler

--
Greetings from overcast Amsterdam

Jan

email: bij...@worldonline.nl
http://home.worldonline.nl/~bijster

0 new messages