1. Home
  2. Computing & Technology
  3. Delphi Programming

Generic Solution to Coloring the Focused Entry Control
RTTI to the Rescue!

By , About.com Guide

Need to change the background color of the focused Delphi data entry control. No problem! Once you uncover the hidden gems of the TScreen object, you dig into more powerful RTTI...

The implementation of the Screen's OnActiveControlChange event handler calls the custom "EnterColor" procedure for the currently focused data entry control after a call to "ExitColor" for the previuosly selected one:

procedure TMainForm.EnterColor(Sender: TWinControl) ;
begin
   if Sender <> nil then
   begin
     if IsPublishedProp(Sender,'Color') then
     begin
       originalColor := GetOrdProp(Sender,'Color') ;
       SetOrdProp(Sender,'Color', focusColor) ;
     end;
   end;
end;
Delphi's RTTI methods are hidden inside the "TypInfo" unit - make sure you add "TypInfo" to the uses clause of the unit where RTTI methods are used.

First you make sure that the "Color" property is exposed by "Sender" using the "IsPublishedProp" procedure. Next, you store the original background color value using the "GetOrdProp" procedure. Finally, the "SetOrdProp" changes the background color to the color we specified to be used for focused controls.

You also need to make sure that the control that had focus has its background color restored:

procedure TMainForm.ExitColor(Sender: TWinControl) ;
begin
   if Sender <> nil then
   begin
     if IsPublishedProp(Sender,'Color') then
     begin
       SetOrdProp(Sender,'Color',originalColor) ;
     end;
   end;
end;
That's all folks!

Make sure you download the full source code.

Explore Delphi Programming
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Using VCL Components
  5. TEdit, TMaskEdit
  6. Generic Solution to Coloring the Focused Data Entry Delphi Control

©2010 About.com, a part of The New York Times Company.

All rights reserved.