Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 359
  • Last Modified:

tedit font color change in enabled=false state

Hi there,

I'd like to change the font color of a tedit component when the enabled flag is set to false. I have found several solutions here so change the background color, however it seems that the font color, depending on the background color is either set to grey or black, however I want it set to clBlack even when enabled set to false.
0
Ishwood
Asked:
Ishwood
1 Solution
 
FSIFMCommented:
I think you need to use the SetSysColors API function.
0
 
AmigoJackCommented:
what FSIFM suggests also involves that all other windows then will use that color, be aware of that.

but when i understood you right, you could also live with the following workaround which i used always: instead of setting enabled:= false set readonly:= true - this has two advantages: the user can still select the content and copy it to clipboard AND youre able to set both the background and textcolor fully according to your needs.
0
 
mokuleCommented:
If You want to avoid grey font color in disabled Edit place it on a Panel.
Then don't disable Edit but disable Panel.
0
 
Eddie ShipmanAll-around developerCommented:
This allows you to set the Font AND background color of all disabled edits on the form...

unit udedits;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TEdit = class(StdCtrls.TEdit)
  private
    FEnabled: Boolean;
    FDisabledColor: TColor;
    FDisabledFontColor: TColor;
    FOldColor, FOldFontColor: TColor;
    procedure SetEnabled(AValue: Boolean);
    procedure SetDisabledColor(AValue: TColor);
    procedure SetDisabledFontColor(AValue: TColor);
    procedure SetReadOnly(AValue: 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;

  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


constructor TEdit.Create(AOwner: TComponent);
begin
  FEnabled := True;
  FDisabledColor := clYellow;
  FDisabledFontColor := clBlack;
  inherited Create(AOwner);
end;

procedure TEdit.SetEnabled(AValue: Boolean);
begin
  if FEnabled <> AValue then
  begin
    inherited ReadOnly := not AValue;
    FEnabled := AValue;
    if AValue then
    begin
      Color := FOldColor;
      Font.Color := FOldFontColor;
    end
    else
    begin
      FOldColor := Color;
      FOldFontColor := Font.Color;
      Color := FDisabledColor;
      Font.Color := FDisabledFontColor;
    end;
  end;
end;

procedure TEdit.SetDisabledColor(AValue: TColor);
begin
  if FDisabledColor <> AValue then
  begin
    if not FEnabled then
      Color := AValue;
    FDisabledColor := AValue;
  end;
end;

procedure TEdit.SetDisabledFontColor(AValue: TColor);
begin
  if FDisabledFontColor <> AValue then
  begin
    if not FEnabled then
      Font.Color := AValue;
    FDisabledFontColor := AValue;
  end;
end;

procedure TEdit.SetReadOnly(AValue: Boolean);
begin
  if FEnabled then
    inherited ReadOnly := AValue;
end;

function TEdit.GetReadOnly: Boolean;
begin
  Result := inherited ReadOnly;
end;

end.
0
 
saravananvgCommented:
Hello Sir,

  Instead of disabling why dont you set the readonly property to true.

with regards,
padmaja.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now