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

Custom colors for selected item in a TListView

3,427 views
Skip to first unread message

Marc Weichhold

unread,
Oct 12, 2004, 3:01:55 AM10/12/04
to
Hello NG.

I have a TListView in report style and multiple selection is not allowed.
Using the (Advanced)CustomDrawItem and (Advanced)CustomDrawSubItem functions
of TListView, I can set the font- and background-colors for the items and
subitems. However, it seems to be impossible to change the default color
(white text on dark-blue background) for the selected item with these
functions. I have read some postings in the usenet and many say that code
like this would work:

procedure TfrmAddBib.MyListViewAdvancedCustomDrawItem(Sender:
TCustomListView; Item: TListItem; State: TCustomDrawState; Stage:
TCustomDrawStage; var DefaultDraw: Boolean);
begin
if (Item.Selected) then
begin
MyListView.Canvas.Brush.Color := clGreen;
MyListView.Canvas.Text.Color := clRed;
end
else
begin
MyListView.Canvas.Brush.Color := clWhite;
MyListView.Canvas.Text.Color := clBlue;
end;
end;

But it does not work... With this code I still get the
white-on-blue-selection bar. I also tried to work with the State variable
like
if (cdsFocused in State) then
...
but nothing works.

Anyone with an idea?

M. Weichhold

PS: I do not want to use third-party components, a little push into the
right direction with the VCL-TListView would help a lot already, I guess.
Thanx. :-)


frederick

unread,
Oct 12, 2004, 11:56:36 PM10/12/04
to

"Marc Weichhold" <weic...@gmx.de> wrote in message
news:416b...@newsgroups.borland.com...

> Hello NG.
>
> I have a TListView in report style and multiple selection is not allowed.
> Using the (Advanced)CustomDrawItem and (Advanced)CustomDrawSubItem functions
> of TListView, I can set the font- and background-colors for the items and
> subitems. However, it seems to be impossible to change the default color
> (white text on dark-blue background) for the selected item with these
> functions. I have read some postings in the usenet and many say that code
> like this would work:
<snip>


procedure TForm1.ListView1AdvancedCustomDrawItem(Sender: TCustomListView;


Item: TListItem; State: TCustomDrawState; Stage: TCustomDrawStage;
var DefaultDraw: Boolean);
begin

if item.Focused then begin
sender.Canvas.Font.Color := clRed;
sender.Canvas.brush.Color := clYellow;
sender.Canvas.Font.Style := [fsbold];
listview1.ItemIndex := -1;
end;
end;

This "deselects" all items - so that you can see the colour of the "selected"
item (if that makes sense). Seems to work fine (D7). You don't need to reset
item.canvas properties to default. Setting the value of a global variable from
the Item.Index on the Listview's OnClick event is an easy way to make sure that
you don't "lose" the selected item.

frederick

unread,
Oct 14, 2004, 5:33:34 PM10/14/04
to

"Marc Weichhold" <webm...@marcweichhold.de> wrote in message
news:b8c7ee9f.04101...@posting.google.com...
> Hello.
>
>> listview1.ItemIndex := -1;

>>
>> This "deselects" all items - so that you can see the colour of the "selected"
>> item (if that makes sense).
>
> Well, no that does not make much sense since I need to keep the
> selection and the selection bar as well.
>
> Marc Weichhold

You do keep the "selection" and the "selection bar" from the user's perspective.

If you are using D7, then forget my comments about having to save the index of
the selected item in a var. Using listview1.ItemIndex := -1 does not deselect
the item. It works fine - the item shows with the correct colours - you get the
selection and "selection bar" appearance.
But - if you are using D5, then you cannot use listview1.ItemIndex := -1. I can
get it to work in D5 by replacing that line with
Listview1.Items.Item[Item.Index].Selected := False ; - but this really does
deselect the item (the user doesn't see this - the colour change indicates that
the item "seems" selected. So in that case, unless there is another answer,
then you do need to save the index of the item that the user selected - if you
are to use it in another procedure:
...
var
Form1: TForm1;
MyIndex : Integer = -1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
If MyIndex >= 0 then
showmessage(listView1.Items.Item[MyIndex].Caption);
end;

procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
begin

if item.Focused then begin
sender.Canvas.Font.Color := clRed;

sender.Canvas.brush.Color := clGreen;
MyIndex := Item.Index;
Listview1.Items.Item[Item.Index].Selected := False ;
end;
end;
...

BTW - clRed text on a clGreen backround is not very good for a "significant
minority" of users. I am slightly red/green colour blind (like 10% of the male
and 1%? of the female population). Normally it is something I do not notice,
but with those colors I cannot read the text unless it is very large.


Marc Weichhold

unread,
Oct 19, 2004, 6:27:57 AM10/19/04
to
Hello.

> You do keep the "selection" and the "selection bar" from the user's perspective.
>
> If you are using D7, then forget my comments about having to save the index of
> the selected item in a var. Using listview1.ItemIndex := -1 does not deselect
> the item. It works fine - the item shows with the correct colours - you get the
> selection and "selection bar" appearance.

I think I understand what you mean, yes this could work. I will try
it, thanx.

> BTW - clRed text on a clGreen backround is not very good for a "significant
> minority" of users. I am slightly red/green colour blind

If you are not red/green color blind, reading red text on a green
background will make you! It looks terrible. ;-) No seriously, I just
chose these colors as a test, I wanted to see a difference to the
normal colors. I do not know yet what colors I will use in the end.

Marc

0 new messages