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 set colours for all my controls? 4

Status
Not open for further replies.

TimSNL

Programmer
Sep 11, 2001
119
AU
Hello everyone,

I would like my application to have a seperate colour scheme from windows. So the user can select the desired colours from the Preferences section of my application.

This my question ...
Do I need to set the coulur of every component in the OnCreate procedure?
Is there a way to point the Color property of a visual component at a variable at design time?

I have many forms in my app, so I am keen to hear any ideas people may have to make this process easier [afro].

Thanks for your comments,
Tim Dover
SNL Computing
 
hi,

I had the same problem, so i wrote a function for coloring a form the only thing you have to do is to put a call into the Oncreate event of a form. The actual code is very long so i will give you an idea how to proceed. The procedure will iretate through a form and will the color the component to the desired color. In this example the colors are defined as constants. If you want to add a new component
you copy the next part.

if Components.ClassNameIs('TEdit') then
begin
(Components as Tedit).Color := SDCEditColor ;
(Components as Tedit).Font.Color := SDCFontColor;
(Components as Tedit).ctl3d := False;
end;

You change Tedit with the component of your choice. Look out the component must have the property you want to change.
So you not only can set the colors but also the other properties.

Steph.

The Code:


const
SDCEditColor = $00FFFF80; //clInfoBk;
SDCErrorColor = clRed;
SDCErrorFontColor = clWhite;
SDCFontColor = clBlack;
SDCGridFontColor = clBlack;
SDCGridFixedColor = $00FFA000; //clYellow;
SDCFormColor = $00EAC593; //clBtnface; //$008E6C04
SDCFormFontColor = $00804000;
SDCBackGrdColor = $00FFA000;

procedure ColorForm(sender :TObject);
var
i: integer;
begin
(Sender as Tform).Font.Color := SDCFormFontColor;
with (Sender as TForm) do
for i := 0 to componentcount -1 do
begin
if Components.ClassNameIs('TEdit') then
begin
(Components as Tedit).Color := SDCEditColor ;
(Components as Tedit).Font.Color := SDCFontColor;
(Components as Tedit).ctl3d := False;
end;
if Components.ClassNameIs('TRxLookupEdit') then
begin
(Components as TRxLookupEdit).Color := SDCEditColor ;
(Components as TRxLookupEdit).Font.Color := SDCFontColor;
(Components as TRxLookupEdit).ctl3d := False;
end;
if Components.ClassNameIs('TRadiogroup') then
begin
(Components as TRadiogroup).Color := SDCFormColor ;
(Components as TRadiogroup).Font.Color := SDCFormFontColor;
end;
if Components.ClassNameIs('TDBEdit')then
begin
(Components as TDBEdit).Color := SDCEditColor ;
(Components as TDBEdit).Font.Color := SDCFontColor;
(Components as TDBedit).ctl3d := False;
end;
if Components.ClassNameIs('TDCFileNameEdit')then
begin
(Components as TDCFileNameEdit).Color := SDCEditColor ;
(Components as TDCFileNameEdit).Font.Color := SDCFontColor;
(Components as TDCFileNameEdit).ctl3d := False;
end;
if Components.ClassNameIs('TDCDirectoryEdit')then
begin
(Components as TDCDirectoryEdit).Color := SDCEditColor ;
(Components as TDCDirectoryEdit).Font.Color := SDCFontColor;
(Components as TDCDirectoryEdit).ctl3d := False;
end;
if Components.ClassNameIs('TDBMemo')then
begin
(Components as TDBMemo).Color := SDCEditColor ;
(Components as TDBMemo).Font.Color := SDCFontColor;
(Components as TDBMemo).ctl3d := False;
end;
if Components.ClassNameIs('TDBCombobox') then
begin
(Components as TDBCombobox).Color := SDCEditColor ;
(Components as TDBCombobox).Font.Color := SDCFontColor;
(Components as TDBCombobox).ctl3d := False;
end;
if Components.ClassNameIs('TCombobox') then
begin
(Components as TCombobox).Color := SDCEditColor ;
(Components as TCombobox).Font.Color := SDCFontColor;
(Components as TCombobox).ctl3d := False;
end;
if Components.ClassNameIs('TDBLookupCombobox')then
begin
(Components as TDBLookupCombobox).Color := SDCEditColor ;
(Components as TDBLookupCombobox).Font.Color := SDCFontColor;
(Components as TDBLookupCombobox).ctl3d := False;
end;
if Components.ClassNameIs('TDBGrid')then
begin
(Components as TDBGrid).Color := SDCEditColor ;
(Components as TDBGrid).Font.Color := SDCGridFontColor;
(Components as TDBGrid).FixedColor := SDCGridFixedColor;
(Components as TDBGrid).ctl3d := False;
end;
if Components.ClassNameIs('TwwDBGrid')then
begin
// (Components as TwwDBGrid).Color := SDCEditColor ;
(Components as TwwDBGrid).Font.Color := SDCGridFontColor;
(Components as TwwDBGrid).TitleColor := SDCGridFixedColor;
end;
if Components.ClassNameIs('TStaticText')then
begin
(Components as TStaticText).Color := SDCBackGrdColor ;
(Components as TStaticText).Font.Color := SDCFontColor;
end;
end;
end;
 
To a certain extent, could one not have :-

for i := 0 to ComponentCount-1 do
begin
if <not special control> then
begin
(Components as TWinControl).Font.color := ...
(Components as TWinControl).color := ... etc
end;
end;

lou
[worm]
p.s. haven't tested this.
 
Thankyou very much for your help, that is a great idea [thumbsup2].

But I have a question about your code...
TForm.Components is an array, so where is the index in your code? How does TForm.Components know which component in the list you are referring to?
 
weez meant to say:
Code:
for i := 0 to ComponentCount-1 do
[...]
    (Components[i] as TWinControl).Font.color := ...
, but the dopey TGML translator deleted his
Code:
[i]
, 'cos it thought it was an italics tag.
 
His?
Only Lou[weez] knows?,
But then again what do I know?, I diddnt realise that a Handle is supposed to disguise your identity (the result of being too old I suppose).

Steve.
 
(Sorry but this is off topic)
hi Steve
I was going to have my name for the handle, but all the obvious handles had gone. 'Weez' is what my brother used to call me when we were sprogs as he couldn't pronounce 'louise', so there you go. Will put this in my profile.

lou


 
(Similarly offtopic)

I made a bum assumption about your gender; figuring &quot;Lou&quot; was short for &quot;Louis&quot;.

D'oh.

Sorry.
 
you there weez?
Code:
(Components[i] as TWinControl).Color := ...
does not seem to work, it says:
[Error] Undeclared identifier: 'Color'

Maybe I can t access color at this level?
I would like to make this a generic portion of code if I can, not being to specific about what type I am dealing with.

Can anyone help me here? [yoda].

Tim
 
Go to the help for TControl. Click on Properties. Color has a little yellow square next to it; i.e. it is a protected property. Looking in TWinControl (a descendant of TControl), we see that Color is not listed at all. We can't set or read a protected property, nor one that isn't listed; we have to go down the hierarchy to a subclass which publishes this inherited property, and get or set it from there.

We could write code like the following:
Code:
    for i := 0 to ComponentCount - 1 do
        if Components[i] is TControl then
            (Components[i] as TControl).Enabled := False;
, because Enabled is published in TControl (it has a green square next to it in the help Properties listing).

The only way I can see to solve your problem is to dig out the Inconveniently Large Object Hierarchy poster, follow down the hierarchy to classes that do publish Color, and set it from there:
Code:
    for i := 0 to ComponentCount - 1 do begin
        if Components[i] is TCheckBox then
            (Components[i] as TCheckBox).Color := clBlack;
        if Components[i] is TEdit then
            (Components[i] as TEdit).Color := clBlack;
    end;
, etc. Bleah. Anybody have a nicer way? -- Doug Burbidge mailto:doug@ultrazone.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top