Solved

How to change the color of the text at runtime

Posted on 2002-07-10
13
335 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
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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
problem when i try to pack my dll file with upx 9 71
How to fill array with TArray.Create? 14 79
delphi parse string to params 3 100
Dev Express grid collapse 2 39
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now