- - * - WhiteUnicorn - * - -




* #WhiteUnicorn/ StartPage/ Documentation/DelphiFAQ >


3239:Graying Out Enabled/Disabled Data Aware Controls

KEYWORDS: disable, controls, components, enable AREA: Application Interop

Graying out enabled/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.

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

Below is the actual procedure to put in the implementation
section of your unit:

// This procedure will either set the text color of a
// dataware control to gray or the user defined color
// constant in clInfoText.

procedure SetDBControlColor(aControl: TControl);
 var
  FontPropInfo: PPropInfo;
 begin
// Check to see if the control is a dataware control
   if (GetPropInfo(aControl.ClassInfo, 'DataSource') = nil) then exit
   else
     begin
// Extract the front property
       FontPropInfo:= GetPropInfo(aControl.ClassInfo, 'Font');
// Check if the control is enabled/disabled
       if (aControl.Enabled = false) then
// If disabled, set the font color to gray
           TFont(GetOrdProp(aControl, FontPropInfo)).Color:= clGrayText
       else
// If enabled, set the font color to clInfoText
           TFont(GetOrdProp(aControl, FontPropInfo)).Color:= clInfoText;
      end;
 end;

The code for the buttonclick event handler should contain:

//  This code will cycle through the Controls array and call
//  SetDbControlColor for each control on your form
//  making sure the font text color is set to what it
//  should be.

procedure TForm1.Button1Click(Sender: TObject);
 var
  i: integer;
begin
// Loop through the control array
  for i:= 0 to ControlCount-1 do
      SetDBControlColor(Controls[i]);
end;

        TI



* #WhiteUnicorn/ StartPage/ Documentation/DelphiFAQ >



- - * - Anastasija aka WhiteUnicorn - * - - LJLiveJournal
PFPhotoFile