Board index » delphi » ListBox Item Color Text...

ListBox Item Color Text...

Pete Harris, I'm using that code, but it isn't works, why?

var
     n:Integer;
begin
     with (Control as TCheckListBox), Canvas, Font do
     begin
         FillRect(Rect);
         Color:= clBlue;

         for n := 0 to CheckListBoxCampos.Items.Count-1 do
         begin
              if(DataModule1.ADOQuery1.Recordset.Fields[n].Properties['KEYCOLUMN'].Value)then
                   TextOut(Rect.Left, Rect.Top, (Control as TCheckListBox).Items[n]);
         end;
     end;
end;

 

Re:ListBox Item Color Text...


Hi Henrique,

Quote
> begin
>      with (Control as TCheckListBox), Canvas, Font do
>      begin
>          FillRect(Rect);
>          Color:= clBlue;

I suppose the With clause should read:

with (Control as TCheckListBox).Canvas.Font do

Thrse

Re:ListBox Item Color Text...


Quote
Henrique wrote:
> Pete Harris, I'm using that code, but it isn't works, why?

> var
>      n:Integer;
> begin
>      with (Control as TCheckListBox), Canvas, Font do
>      begin
>          FillRect(Rect);
>          Color:= clBlue;

>          for n := 0 to CheckListBoxCampos.Items.Count-1 do
>          begin
>               if(DataModule1.ADOQuery1.Recordset.Fields[n].Properties['KEYCOLUMN'].Value)then
>                    TextOut(Rect.Left, Rect.Top, (Control as TCheckListBox).Items[n]);
>          end;
>      end;
> end;

Try loosing the with statement

CheckListBoxCampos.Canvas.FillRect(Rect);
CheckListBoxCampos.Canvas.Font.Color:= clBlue;

for n := 0 to CheckListBoxCampos.Items.Count-1 do
begin
  if(DataModule1.ADOQuery1.Recordset.Fields[n].Properties['KEYCOLUMN'].Value)then
     CheckListBoxCampos.Canvas.TextOut(Rect.Left, Rect.Top, CheckListBoxCampos.Items[n]);
end;

Also, depending on your if condition the text will not be drawn every time.

What I think you need to do is set the color with the condition.

if(DataModule1.ADOQuery1.Recordset.Fields[n].Properties['KEYCOLUMN'].Value)then
  CheckListBoxCampos.Canvas.Font.Color:= clRed
else
  CheckListBoxCampos.Canvas.Font.Color:= clBlue;

CheckListBoxCampos.Canvas.TextOut(Rect.Left, Rect.Top, CheckListBoxCampos.Items[n]);

Re:ListBox Item Color Text...


The DrawItem method gets called for each item in the list box, so you
must deal with one item at a time. You cannot put your FOR loop inside
the method. Also, what do you want to happen if DataModule1...Value is
false? With your code, those items won't display at all.

Also, I was rather heavy-handed with the WITH statement. My suggestion
will be less fragile using something like...

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect;
  State: TOwnerDrawState);
begin
  with (Control as TListBox) do begin
    Canvas.FillRect(Rect);
    if
DataModule1.ADOQuery1.Recordset.Fields[Index].Properties['KEYCOLUMN'].Value
then
      Canvas.Font.Color:= clBlue
    else
      Canvas.Font.Color:= clRed;
    Canvas.TextOut(Rect.Left, Rect.Top, Items[Index]);    
    end;
end;

Hope this helps.

Quote
Henrique wrote:

> Pete Harris, I'm using that code, but it isn't works, why?

> var
>      n:Integer;
> begin
>      with (Control as TCheckListBox), Canvas, Font do
>      begin
>          FillRect(Rect);
>          Color:= clBlue;

>          for n := 0 to CheckListBoxCampos.Items.Count-1 do
>          begin
>               if(DataModule1.ADOQuery1.Recordset.Fields[n].Properties['KEYCOLUMN'].Value)then
>                    TextOut(Rect.Left, Rect.Top, (Control as TCheckListBox).Items[n]);
>          end;
>      end;
> end;

Other Threads