Board index » delphi » Change disabled object's text color

Change disabled object's text color

Hi,
I have an EditBox with its Enabled property set to false, but I want the
text in it to be displayed in another color (other than the standard). How
can I change it?

Thanks

Gilles Baatsen

 

Re:Change disabled object's text color


Simple solution, place the control on a panel with no borders, disable
the panel rather than the text box (which prevents the user entering
text) and change the teditbox.color and teditbox.font.color properties
as you want.

or for a small control you can drop on a form - similar to a control
which I use frequently. Add your own handlers for other controls rather
than edit boxes or to handle colors

Type TDisablePanel = class(TPanel)
private
  fEnabled : Boolean;
  procedure SetColors(Control : TWinControl);
  procedure SetEnabled(Value : Boolean);
public
  constructor Create(AOwner : TComponent);override;
published
  property Enabled : Boolean read fEnabled write SetEnabled;
end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponent(TDisablePanel,'Samples');
end;

constructor TDisablePanel.Create(AOwner : TComponent);
begin
  inherited;
  if not (csDesigning in componentState) then BevelOuter := bvNone;
  fEnabled := inherited Enabled;
end;

procedure TDisablePanel.SetColors(Control : TWinControl);
var
  nN : Integer;
begin
  For nN := 0 to Control.ControlCount - 1 do begin
    // Change the children of this child control
    if Control.Controls[nN] is TWinControl then begin
      SetColors(TWinControl(Control.Controls[nN]));
    end;
  end;
  // Change this control
  if Control is TEdit then begin
    if enabled then begin
      TEdit(Control).Color := clWhite;
      TEdit(Control).Font.Color := clBlack;
    end else begin
      TEdit(Control).Color := clRed;
      TEdit(Control).Font.Color := clWhite;
    end;
  end;
end;

procedure TDisablePanel.SetEnabled(Value : Boolean);
begin
  SetColors(Self);
  fEnabled := Value;
  inherited enabled := Value;
end;

Kind Regards
Claire, c...@HallworthHome.demon.co.uk

Other Threads