Jump to content
Sign in to follow this  
Dave Nottage

TEdit with a background color

Recommended Posts

Instead of using Edit Custom Style for every component that we want a background color on, I came up with some code similar to this:

type
  TEditEx = class(TEdit)
  private
    FColor: TAlphaColor;
    function GetBackgroundRectangle: TRectangle;
    procedure InternalSetColor(const Value: TAlphaColor);
    procedure SetColor(const Value: TAlphaColor);
  protected
    procedure ApplyStyle; override;
  published
    property Color: TAlphaColor read FColor write SetColor;
  end;
procedure TEditEx.ApplyStyle;
begin
  inherited;
  InternalSetColor(FColor);
end;

function TEditEx.GetBackgroundRectangle: TRectangle;
var
  LStyleObject: TFmxObject;
  I: Integer;
begin
  Result := nil;
  LStyleObject := FindStyleResource('rect');
  if LStyleObject = nil then
  begin
    LStyleObject := FindStyleResource('background');
    if LStyleObject <> nil then
    begin
      Result := TRectangle.Create(LStyleObject);
      Result.StyleName := 'rect';
      Result.Align := TAlignLayout.Contents;
      Result.HitTest := False;
      Result.Stroke.Kind := TBrushKind.None;
      Result.Fill.Color := TAlphaColorRec.Null;
      Result.Parent := LStyleObject;
    end;
  end
  else
    Result := TRectangle(LStyleObject);
end;

procedure TEditEx.InternalSetColor(const Value: TAlphaColor);
var
  LRectangle: TRectangle;
begin
  LRectangle := GetBackgroundRectangle;
  if LRectangle <> nil then
  begin
    FColor := Value;
    LRectangle.Fill.Color := FColor;
  end;
end;

procedure TEditEx.SetColor(const Value: TAlphaColor);
begin
  NeedStyleLookup;
  ApplyStyleLookup;
  InternalSetColor(Value);
end;

A side-effect of this code is that the inner parts of the edit control can be selected in the Object Inspector, like this:
image.thumb.png.454b631f07f1ed62440f593d0c8d737c.png
 

Any ideas of what I've done wrong, and how to remedy it?

Share this post


Link to post

Hi,

 

Try

Result.Locked := True;

 

 

 

Edited by f.m
  • Like 2

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×