colorsdelphi-2010delphi-xein-place-editor

Color property in TInplaceEdit not working


The following code to change the background color of TInplaceEdit works in Delphi 2010 but does not work in Delphi 11 Community Edition. However the CharCase does work. I looked at many samples addressing this problem but none did work for me. What am I doing wrong?

type

  TMyInplaceEdit = class(TInplaceEdit)
  public
    property Color; //redeclare to make public
    constructor Create(AOwner: TComponent); override;
  end;

  TTestGrid = class(TStringGrid)
  public
    property InplaceEditor; //redeclare to make public
    function CreateEditor: TInplaceEdit; override;
  end;

  TForm1 = class(TForm)
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    TestGrid: TTestGrid;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

constructor TMyInplaceEdit.Create(AOwner: TComponent);
begin
  inherited;
  Color := clMoneyGreen;
  CharCase := ecUpperCase;
end;

function TTestGrid.CreateEditor: TInplaceEdit;
begin
  inherited;// same without this line
  Result := TMyInplaceEdit.Create(self);
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  TestGrid := TTestGrid.Create(Self);
  TestGrid.Parent := Self;
  TestGrid.Show;
  TestGrid.Options := [goFixedVertLine, goFixedHorzLine, goVertLine,   goHorzLine, goEditing, goTabs];

end;

Solution

  • First off, there was no Delphi 2011, let alone a Community Edition of it. The next version after Delphi 2010 was Delphi XE. The first Community Edition was Delphi 10.2 Tokyo. Perhaps you meant Delphi 11 instead (ie, the current Community Edition version)?

    In any case, the reason your editor's Color is being ignored is because after CreateEditor() returns, the grid sets the editor's Color to match the grid's Color (it also sets the editor's Font and StyleElements to match the grid's, too).

    This behavior was added in Delphi XE3, which is why it does not happen in Delphi 2010.

    Every time the grid updates the editor, it resets the editor's Color (and Font and StyleElements) to match the grid's values, making it more difficult to customize the editor the way you want. However, I was able to achieve what you want by simply having the editor handle the CM_COLORCHANGED notification to change the Color back to clMoneyGreen, eg:

    type
      TMyInplaceEdit = class(TInplaceEdit)
      protected
        procedure CMColorChanged(var Message: TMessage); message CM_COLORCHANGED;
      ...
      end;
    
    procedure TMyInplaceEdit.CMColorChanged(var Message: TMessage);
    begin
      inherited;
      Color := clMoneyGreen;
      // or here, it doesn't matter either way...
      // inherited;
    end;