Solved

changing color font in disabled edit box

Posted on 1998-12-28
3
461 Views
Last Modified: 2013-12-03
Hi,
is there a way of changing the color of the font in a disabled tedit control? i.e is it possible to have the contents displayed in something different than the grey color to show a disabled status ? (for ex: I want red font over white background when control is disabled)
0
Comment
Question by:fadyg
  • 2
3 Comments
 
LVL 17

Accepted Solution

by:
inthe earned 50 total points
ID: 1353448
yes but it's quite tricky .
i post a component for you to to it cut n paste and save as DiscEdit.Pas then install component it will be on samples pallette :

unit DisCEdit;

interface

uses
  WinTypes, WinProcs, Messages, SysUtils, Classes, Graphics, Controls,
  StdCtrls;

type
  TDisabledColorEdit = class(Tedit)
  private
    { Private declarations }
    FEnabled: Boolean;
    FDisabledColor: Tcolor;
    FDisabledFontColor: TColor;
    FOldColor, FOldFontColor: TColor;

    Procedure SetEnabled( value: Boolean );
    Procedure SetDisabledColor( value: TColor );
    Procedure SetDisabledFontColor( value: TColor );
    Procedure SetReadOnly( value: Boolean );
    Function GetReadOnly: Boolean;
  protected
    { Protected declarations }
  public
    { Public declarations }

    Constructor Create( aOwner: TComponent ); override;
  published
    { Published declarations }

    property Enabled: Boolean read FEnabled write SetEnabled default true;
    property ReadOnly: Boolean read GetReadOnly write SetReadOnly default
false;
    property DisabledColor: TColor read FDisabledColor write
SetDisabledColor;
    property DisabledFontColor: TColor read FDisabledFontColor write
SetDisabledFontColor;
  end;

procedure Register;

implementation

 Constructor TDisabledColorEdit.Create( aOwner: TComponent );
 Begin
   FEnabled := True;
   FDisabledColor := clWindowText;
   FDisabledFontColor := clGrayText;
   inherited Create( aOwner );
 End;

 Procedure TDisabledColorEdit.SetEnabled( value: Boolean );
 Begin
   If FEnabled <> value Then Begin
     inherited ReadOnly := not value;
     FEnabled := value;
     If value Then Begin
       Color := FOldColor;
       Font.Color := FOldFontColor;
     End
     Else Begin
       FOldColor := Color;
       FOldFontColor := Font.Color;
       Color := FDisabledColor;
       Font.Color := FDisabledFontColor;
     End;
   End;
 End;

 Procedure TDisabledColorEdit.SetDisabledColor( value: TColor );
 Begin
   If FDisabledColor <> value Then Begin
     If not FEnabled Then
       Color := value;
     FDisabledColor := value;
   End;
 End;

 Procedure TDisabledColorEdit.SetDisabledFontColor( value: TColor );
 Begin
   If FDisabledFontColor <> value Then Begin
     If not FEnabled Then
       Font.Color := value;
     FDisabledFontColor := value;
   End;
 End;

 Procedure TDisabledColorEdit.SetReadOnly( value: Boolean );
 Begin
   If FEnabled Then
     inherited ReadOnly := value;
 End;

 Function TDisabledColorEdit.GetReadOnly: Boolean;
 Begin
   result := inherited ReadOnly;
 End;

procedure Register;
begin
  RegisterComponents('Samples', [TDisabledColorEdit]);
end;

end.

0
 

Author Comment

by:fadyg
ID: 1353449
thanks for your reply,
your component would work correctly but this would give me a control that can get the focus (even though you won't be able to change anything because it's a read-only).  What is needed is the exact same behaviour as diabled i.e. no focus.

any ideas ?
0
 
LVL 17

Expert Comment

by:inthe
ID: 1353450
ok to try this put my comp on a form and a button
give my comp some disabledcolors then on button click type this:

SetWindowLong(DisabledColorEdit1.Handle,GWL_STYLE,
    GetWindowLong(DisabledColorEdit1.Handle,GWL_STYLE) or WS_DISABLED);
DisabledColorEdit1.Enabled:=False;

this will stop it from recieving focus
if you want it to recieve focus type this:

SetWindowLong(DisabledColorEdit1.Handle,GWL_STYLE,
    GetWindowLong(DisabledColorEdit1.Handle,GWL_STYLE) and not WS_DISABLED);

Regards Barry



0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

743 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

11 Experts available now in Live!

Get 1:1 Help Now