skylla
skylla

Reputation: 464

Override Read and Write of Property of base class

I want to create a new component which behaves exactly like a TEdit, but has some characters replaced of the text that is entered in it. For Example, when someone types 'abc' into the new component, I want the Text-Property to return 'aac', when it is read in the source code.

type
  TMyEdit = class(TEdit)
  public
    property Text : TCaption read GetText;
  end;

Something like this.

Is it possible to override an existing property with a new read - function for this property and not change the write - function for this property?

Regards

Upvotes: 2

Views: 694

Answers (2)

Jens Borrisholt
Jens Borrisholt

Reputation: 6402

As said before :

The best approach is to use TMaskEdit

But if you realy want to implement the behaviour then it could be done like this:

type
  TMyEdit = class(TEdit)
  private
    function GetText: TCaption;
    procedure SetText(const Value: TCaption);
  public
    property Text: TCaption read GetText write SetText;
  end;

{ TMyEdit }

function TMyEdit.GetText: TCaption;
begin
  Result := 'TMyEdit' + inherited Text;
end;

procedure TMyEdit.SetText(const Value: TCaption);
begin
  inherited Text := Value;
end;

So in short I create both GetText and SetText settext just call the inherited Text property while GetText changes the result

Upvotes: 1

Pavlo Golub
Pavlo Golub

Reputation: 399

TMyEdit = class(TEdit)
protected
   procedure Change; override;
 end;

procedure TMyEdit.Change;
begin
  Self.Text := StringReplace(Self.Text, 'aaa', 'ccc');
  inherited;
end;

Upvotes: 0

Related Questions