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

Change the color of a TDBText field on TDBCtrlGrid?

1,459 views
Skip to first unread message

Johan Boshoff

unread,
Oct 4, 1999, 3:00:00 AM10/4/99
to
Hi all,

I am writing a database application that have to display the different
priorities for a call system in a DBCtrlGrid. I want each line of the
DBCtrlGrid to display the priority field in a colour according the value of
the field being displayed e.g.
Priority Normal = Grey
Urgent = Blue
Very Urgent = Red

I am using a TDBText field to display the value of the priority field, but
is unable to change the colour displayed as the user scroll through the
list.

Any ideas on how to to accomplish this?

Johan Boshoff

AlanGLLoyd

unread,
Oct 5, 1999, 3:00:00 AM10/5/99
to
In article <37f87...@news1.mweb.co.za>, "Johan Boshoff" <bos...@pixie.co.za>
writes:

>I am writing a database application that have to display the different
>priorities for a call system in a DBCtrlGrid. I want each line of the
>DBCtrlGrid to display the priority field in a colour according the value of
>the field being displayed e.g.
>Priority Normal = Grey
>Urgent = Blue
>Very Urgent = Red
>
>I am using a TDBText field to display the value of the priority field, but
>is unable to change the colour displayed as the user scroll through the
>list.
>

You will have to make a descendant class to TDBText in order to override its
protected virtual Paint procedure. In that Paint procedue you do your own
drawing of the background and the text.

You also have to access the Panel protected property of the TDBCtrlGrid in
order to allocate the TDBCtrlGrid.Panel as the parent of the TDBColorText. You
do this by type-casting your TDBCtrlGrid to a descendant class of TDBCtrlGrid.

As follows :-

In the form's interface type clause :-

type
TDBColorText = class(TDBText)
procedure Paint; override; // to specify & expose the TDBText.Paint
method
end;

TDBColorCtrlGrid = class(TDBCtrlGrid); // to expose the TDBCtrl.Panel

Specify a variable for the TDBColorText :-

var
MyDBColorText : TDBColorText;

Specify the drawing in the paint procedure :-

procedure TDBColorText.Paint; // note the class prefix
var
InitChar : char;
begin
inherited Paint;
{paint the TDBText appropriately as you wish
I've just coloured them as the initial character}
InitChar := Self.Field.AsString[1];
with Self.Canvas do begin
case InitChar of
'a' : Brush.Color := clRed;
'e' : begin
Brush.Color := clBlue;
Font.Color := clWhite; // white text on blue is clearer
end;
else
Brush.Color := clBtnFace; // standard grey background
end;
FillRect(ClipRect); // paint the background color ...
TextOut(2, 2, Self.Field.AsString); // ... and the text
end; {with Self.Canvas}
end;

Then in your form's FormCreate, create an instance of the new DBColorText,
allocating the panel of the DBCtrlGrid as the parent :-

procedure TForm1.FormCreate(Sender: TObject);
{this is FormCreate of the form the DBCtrlGrid is on}
begin
MyDBColorText := TDBColorText.Create(Self);
with MyDBColorText do begin
Parent := TDBPanelCtrlGrid(DBCtrlGrid1).Panel; // allocate the parent
panel
SetBounds(24, 20, 65, 17); // this is the position & size of the TDBText
on the panel
DataSource := DataSource1; // whatever your datasource is ...
DataField := 'FILE_NAME'; // ... and your data field name
end; {with MyDBColorEdit}
end;

Thats it <g>

Alan Lloyd
alang...@aol.com

AlanGLLoyd

unread,
Oct 5, 1999, 3:00:00 AM10/5/99
to
In article <19991005033843...@ngol02.aol.com>, alang...@aol.com
(AlanGLLoyd) writes:

>You will have to make a descendant class to TDBText in order to override its
>protected virtual Paint procedure.
>

<snip>
>

Ooops - That's a _very_ complicated way to do it - Sorry

Just put your colour handling in the OnPaintPanel event handler of the
DBCtrlGrid :-

procedure TForm1.DBCtrlGrid1PaintPanel(DBCtrlGrid: TDBCtrlGrid; Index:
Integer);
begin
with DBText1 do
case Table1FILE_NAME.AsString[1] of
'a' : begin
Color := clRed;
Font.Color := clBlack;
end;
'e' : begin


Color := clBlue;
Font.Color := clWhite;

end;
else begin
Color := clBtnFace; // set the standard ...
Font.Color := clBlack; // ... back to default
end;
end;
end;


Alan Lloyd
alang...@aol.com

Johan Boshoff

unread,
Oct 5, 1999, 3:00:00 AM10/5/99
to
Thanx Alan,

This worked like a charm.

Johan Boshoff

Johan Boshoff

unread,
Oct 6, 1999, 3:00:00 AM10/6/99
to
This is the final implementation. I added a check to make sure that the
field displayed is not NULL.
procedure TViewCallDlg.DBCtrlGrid1PaintPanel(DBCtrlGrid: TDBCtrlGrid;
Index: Integer);
begin
WITH DBTPriority DO BEGIN
IF NOT Field.IsNull THEN BEGIN
CASE Field.AsString[1] OF
'N' : BEGIN
Color := clLime;
Font.Color := clBlack;
END; (* N *)
'U' : BEGIN

Color := clBlue;
Font.Color := clWhite;
END; (* U *)
'V' : BEGIN

Color := clRed;
Font.Color := clBlack;
END; (* V *)
END; (* case *)
END; (* if *)
END; (* with *)
end;

AlanGLLoyd wrote in message
<19991005033843...@ngol02.aol.com>...


>In article <37f87...@news1.mweb.co.za>, "Johan Boshoff"
<bos...@pixie.co.za>
>writes:
>
>>I am writing a database application that have to display the different
>>priorities for a call system in a DBCtrlGrid. I want each line of the
>>DBCtrlGrid to display the priority field in a colour according the value
of
>>the field being displayed e.g.
>>Priority Normal = Grey
>>Urgent = Blue
>>Very Urgent = Red
>>
>>I am using a TDBText field to display the value of the priority field, but
>>is unable to change the colour displayed as the user scroll through the
>>list.
>>
>

>You will have to make a descendant class to TDBText in order to override
its

eduard iglesias torres

unread,
Feb 13, 2023, 5:33:54 AM2/13/23
to
El dia dimecres, 6 d’octubre de 1999 a les 9:00:00 UTC+2, Johan Boshoff va escriure:
Perfecte!!!! googd!!!
0 new messages