Articles   Members Online: 3
-Article/Tip Search
-News Group Search over 21 Million news group articles.
Member Area
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Become a Member
-Why sign up!
-Chat Online!
-Indexes NEW!!
-Build your resume
-Find a job
-Post a job
-Resume Search
-Link to us
Visit Embarcadero
Embarcadero Community
How to grey-out enabled or disabled data-aware controls Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Delphi 3.x
User Rating
No Votes
# Votes
DSP, Administrator
Reference URL:
			Author: Tomas Rutkauskas 

How to grey-out enabled or disabled data-aware controls


Most data aware components are capable of visually showing that they are disabled 
(by changing the text color to gray) or enabled (by setting the color to a 
user-defined windows text color). Some data aware controls such as TDBGrid, 
TDBRichEdit (in Delphi 3.0) and also TDBEdit (when connected to a numeric or date 
field) do not display this behavior.

The code below uses RTTI (Run Time Type Information) to extract property 
information and use that information to set the font color to gray if the control 
is disabled. If the control is enabled, the text color is set to the standard 
windows text color.

What follows is the step by step creation of a simple example which consists of a 
TForm with a TButton and a TDBRichEdit that demonstrates this behavior.

Select File|New Application from the Delphi menu bar.
Drop a TDataSource, a TTable, a TButton and a TDBEdit onto the form.
Set the DatabaseName property of the table to 'DBDEMOS'.
Set the TableName property of the table to 'ORDERS.DB'.
Set the DataSet property of the datasource to 'Table1'.
Set the DataSource property of the DBEdit to 'DataSource1'.
Set the DataField property of the DBEdit to 'CustNo'.
Set the Active property of the DBEdit to 'False'.
Add 'TypInfo' to the uses clause of the form.

Below is the actual procedure to put in the implementation section of your unit:
2   {This procedure will either set the text color of a dataware control to gray or the
3   user defined color constant in clInfoText}
5   procedure SetDBControlColor(aControl: TControl);
6   var
7     FontPropInfo: PPropInfo;
8   begin
9     {Check to see if the control is a dataware control}
10    if (GetPropInfo(aControl.ClassInfo, 'DataSource') = nil) then
11      exit
12    else
13    begin
14      {Extract the front property}
15      FontPropInfo := GetPropInfo(aControl.ClassInfo, 'Font');
16      {Check if the control is enabled/disabled}
17      if (aControl.Enabled = false) then
18        {If disabled, set the font color to grey}
19        TFont(GetOrdProp(aControl, FontPropInfo)).Color := clGrayText
20      else
21        {If enabled, set the font color to clInfoText}
22        TFont(GetOrdProp(aControl, FontPropInfo)).Color := clInfoText;
23    end;
24  end;

The code for the buttonclick event handler should contain:
26  {This code will cycle through the Controls array and call SetDbControlColor
27  for each control on your form making sure the font text color is set to what
28  it should be}
30  procedure TForm1.Button1Click(Sender: TObject);
31  var
32    i: integer;
33  begin
34    {Loop through the control array}
35    for i := 0 to ControlCount - 1 do
36      SetDBControlColor(Controls[i]);
37  end;

Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10


Share this page
Download from Google

Copyright © Mendozi Enterprises LLC