Solved

How to change the color of the text at runtime

Posted on 2002-07-10
13
346 Views
Last Modified: 2010-04-04
I have written common function to make the components
readonly at runtime. Now i also want to change the color of the components.

My code is like this

PRecProp := GetPropInfo(Components  
                        [intI].ClassInfo, 'ReadOnly');
if Assigned(PRecProp) then begin
   SetOrdProp(Components[intI], PRecProp, Integer(not
                                      (blnValue)));
 

How can i do that?

Rajni:)
0
Comment
Question by:rajniyadav
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
13 Comments
 
LVL 17

Expert Comment

by:geobul
ID: 7145691
Hi,

PRecProp := GetPropInfo(Components  
                       [intI].ClassInfo, 'Color');
if Assigned(PRecProp) then begin
  SetOrdProp(Components[intI], PRecProp, clRed);

Regards, Geo
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7146198
instead of to make it readonly, just set the enabled-propery to false,
the textcolor depending on the control is then normally grayed automatically

meikl ;-)
0
 

Author Comment

by:rajniyadav
ID: 7148224
geobul

I cannot user color property because that will change the background color of the components. I want to change the text color of the component i.e Font.Color

kretzschmar

I cannot enable or disable control because if the text in it is more than the length of the component then i cannot move forward to see the remaining text.

rajni

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:BaSoAM
ID: 7148253
If you're just changing the color of the text and not making them read-only then you can still use the Show Hint property of the editbox.  

edit1.showhint := true;
edit1.hint := edit1.text;

this way you won't need to scroll to see the whole thing and is a better interface.  You can just move mouse over the editbox and without additional steps you can read the entire input.
0
 
LVL 17

Expert Comment

by:geobul
ID: 7148511
Hi,

PRecProp := GetPropInfo(Components[intI].Font.ClassInfo, 'Color');
if Assigned(PRecProp) then begin
 SetOrdProp(Components[intI].Font, PRecProp, clGray);

Regards, Geo
0
 

Author Comment

by:rajniyadav
ID: 7148608
geobul

I tried your code but it give error for Component[inti].Font. It says undeclared identifier.


Rajni
0
 
LVL 10

Expert Comment

by:kacor
ID: 7148943
Hi Rajni,

you typed it wrong. You have to write INTL instead of INTI

regards

Janos
0
 

Author Comment

by:rajniyadav
ID: 7148979
Janos

I am using intI variable in my code instead of intL so that's not the case

Rajni

0
 
LVL 17

Expert Comment

by:geobul
ID: 7149032
Well, I supposed that you had some type checking before that code like if .. is TEdit then ..

Font property is introduced in TControl, so:

if (Components[intI] is TControl) then begin
  PRecProp := GetPropInfo((Components[intI] as TControl).Font.ClassInfo, 'Color');
  if Assigned(PRecProp) then SetOrdProp((Components[intI] as TControl).Font, PRecProp, clGray);
end;

Regards, Geo
0
 

Author Comment

by:rajniyadav
ID: 7153581
geobul

I am writing this but still it gives me the same error
Undeclared identifier 'Font'
 
if (Components[intI] is TControl) then begin
  PRecProp := GetPropInfo((Components[intI] as  
                    TControl).Font.ClassInfo, 'Color');
  if Assigned(PRecProp) then begin
    SetOrdProp((Components[intI] as
             TControl).Font, PRecProp, clGray);
  end;
end;


Rajni
0
 
LVL 17

Accepted Solution

by:
geobul earned 50 total points
ID: 7153672
I'm sorry, my mistake. TControl.Font property is a protected one and therefore not visible. So the solution is:

  // define a new class used for typecasting only
  THackControl = class(TControl)
  public
    property Font;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

uses TypInfo;

function SetFontColor(fForm: TForm; sControl: string; value: integer): integer;
var
  i: integer;
  PRecProp: PPropInfo;
begin
 result := 0;
 try
  for i := 0 to fForm.ComponentCount - 1 do
    if (fForm.Components[i] is TControl) then
      if (fForm.Components[i]).Name = sControl then begin
        PRecProp := GetPropInfo(THackControl(fForm.Components[i]).Font.ClassInfo, 'Color');
        if Assigned(PRecProp) then
           SetOrdProp(THackControl(fForm.Components[i]).Font, PRecProp, value);
        break;
      end;
 except
   result := 1;
 end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SetFontColor(Form1,'Label1', clRed);
  SetFontColor(Form1,'Edit1', clRed);
end;

Regards, Geo
0
 

Author Comment

by:rajniyadav
ID: 7168728
geobul

your solution works thanx a lot

Rajni
0
 
LVL 17

Expert Comment

by:geobul
ID: 7169143
The pleasure was mine :-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question