Solved

How to change the color of the text at runtime

Posted on 2002-07-10
13
332 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

759 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

18 Experts available now in Live!

Get 1:1 Help Now