Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to control font color in a TDBTxt component. 1

Status
Not open for further replies.

delphiman

Programmer
Dec 13, 2001
422
ZA

I need to control this to be Black for a "+" value
and Red for a "-" value.

Please anyone?
 
You could try coding a GetText event handler for the appropriate TField.

It might look something like this where the cash field is being displayed in DBEdit1:
Code:
procedure TForm1.Table1CashGetText(Sender: TField; var Text: String;
  DisplayText: Boolean);
begin
  if Sender.AsInteger < 0 then
    DBEdit1.Color := clRed
  else
    DBEdit1.Color := clBlack;
  Text := Sender.AsString;
end;

Andrew
 
The code povided by Towerbase is pretty much correct but you would want to call :-

DBEdit1.Font.Color := clred

otherwise you are just going to change the background color of the DBEditbox.


When your feeling down and your resistance is low, light another cigarette and let yourself go [rockband]
 
Thanks Eric. Although I tested my code out, I actually prefer to change the cell background colour as this makes the negative bit much more obvious especially for small strings and for people who don't have very good colour vision. But you are quite right, delphiman did ask for the ability to change the font.

Andrew
 
You are both good lads!!
Thanks a stack for that and do have a Happy Festive season.
Remembering to keep death off the roads - by driving on the sidewalks! Also refrain from wasting water - by drinking wine instead!

 
Andrew!
I don't think I have a proper grasp of how one calls Procedures.

On the one hand the following works for me.

private
procedure DoSomething;
{ Private declarations }
public
{ Public declarations }
end;


Etc. etc.


procedure TdmStdAcc.DoSomething;
begin
qryMyQuery.Open;
try
// ShowMessage ('I get here');
finally
end;
end;

procedure Whatever.qrySomethingChange(Sender: TField)
begin

// Stuff

DoSomething;
end;


..... but on the other hand I find myself out of my depth with your suggested ...


procedure TForm1.Table1CashGetText(Sender: TField; var Text: String;
DisplayText: Boolean);
begin
if Sender.AsInteger < 0 then
DBEdit1.Color := clRed
else
DBEdit1.Color := clBlack;
Text := Sender.AsString;
end;


Could you please throw some light on how I should &quot;call&quot; this procedure?
Including how I should declare it in Private

Thanks in advance.
 
You need to have a TField object and associated GetText event handler for the field in question.

If you are using TTable the TField object can be created easily by clicking on the TTable icon and doing Ctrl+F. Use the Object Inspector to create a GetText event handler.

Alternatively you can define a TField object and event handler in your private declarations:
Code:
  private
    { Private declarations }
    Cash: TField;
    procedure CashGetText ( sender: TField; var text: string; displaytext: boolean );
If you are doing this &quot;by hand&quot; you need to point Cash at the real TField object by doing something like this in your form's OnCreate event handler.
Code:
  Cash := Table1.FieldByName('bells');
  Cash.OnGetText := CashGetText;
  Table1.Open;    // assuming Cash is a field in Table1.

The GetText event handler should look something like:
Code:
procedure TForm1.CashGetText(sender: TField; var text: string;
  displaytext: boolean);
begin
if Sender.AsInteger < 0 then
    DBText1.Color := clRed
  else
    DBText1.Color := clWhite;
  Text := Sender.AsString;
end;
Note that in my first reply I mistakenly used TDBEdit instead of TDBText.

Andrew
 
Thanks Andrew you really are a great credit to this forum with your contributions.

One further question ....

How does one (can one? ) call a Function or a Procedure (such as this)from another form within the same Project?

Thanks in advance.
 
Thank you for the star :)

The CashGetText procedure will automatically get called when Windows needs to paint the (TDBText) control. You should not need to call the CashGetText procedure from another form.

It may be sensible to have a general purpose function if you are likely to colour code several controls in this way. Then if your requirements change so that, say, positive values must be green then you would only have one place to modify the code.

If you mean how, in general, do I call procedures in Form1 from Form2 then simply ensure that you have an appropriate uses such as
Code:
Uses ..., ..., Form1;
in Form2 and call the procedure like this:
Code:
  Form1.MyProcedure ( param1, param2 ... );
Note that the MyProcedure should not be defined in the private declaration section of TForm1 otherwise it will be hidden.

Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top