Jump to content
karl Jonson

TMemo instead of TLabel

Recommended Posts

Hi,
I'm using a procedure which uses a TLabel parameter & sets the caption of the label:

 

prcedure GetString(param1: string; param2:string; myLabel: TLabel);
Begin
  myLabel.caption := GetDescription()
End


 
Everything works fine & I don't want to break it as it's called from different places.
Now I'd like to be able to sometimes pass it TMemo and display multiple lines in the TMemo component:

 

  myMemo.lines.Add(GetDescription());

I'd like to be able to say if component is TLabel then do this but if it's TMemo then do that.

TIA

Edited by karl Jonson
explanation

Share this post


Link to post
procedure GetString(param1: string; param2:string; myLabel: TLabel); overload;
Begin
  myLabel.caption := GetDescription();
End;

procedure GetString(param1: string; param2:string; myMemo: TMemo); overload;
Begin
  myMemo.lines.Add(GetDescription());
End;

 

  • Like 3

Share this post


Link to post

If I understand your need add a flag to 

GetDescription(FLAG: boolean) so your function will know where it came from

Memo.lines.add(getDescription(true)) or label.caption:=getdescription(false)

Edited by limelect

Share this post


Link to post
8 hours ago, karl Jonson said:

Now I'd like to be able to sometimes pass it TMemo and display multiple lines in the TMemo component:


  myMemo.lines.Add(GetDescription());

I'd like to be able to say if component is TLabel then do this but if it's TMemo then do that.

You can use the 'is' operator for that purpose, eg:

procedure GetString(param1: string; param2: string; myControl: TControl);
begin
  if myControl is TLabel then
    TLabel(myControl).Caption := GetDescription()
  else if myControl is TMemo then
    TMemo(myControl).Lines.Add(GetDescription())
  else
    ...;
end;

Alternatively, every TControl has a Caption (and Text) property, even if it is not published. You can take advantage of that, eg:

type
  TControlAccess = class(TControl)
  end;

procedure GetString(param1: string; param2: string; myControl: TControl);
begin
  if myControl is TMemo then
    TMemo(myControl).Lines.Add(GetDescription())
  else
    TControlAccess(myControl).Caption := GetDescription();
end;

Or, you can use RTTI instead, eg:

procedure GetString(param1: string; param2: string; myControl: TControl);
var
  Ctx: TRttiContext;
  Typ: TRttiType;
  Prop: TRttiProperty;
begin
  Ctx := TRttiContext.Create;
  try
    Typ := Ctx.GetType(myControl.ClassType);
    Prop := Typ.GetProperty('Caption');
    if (Prop <> nil) and (Prop.Visibility = mvPublished) then
    begin
      Prop.SetValue(myControl, GetDescription());
      Exit;
    end;
    Prop := Typ.GetProperty('Text');
    if (Prop <> nil) and (Prop.Visibility = mvPublished) then
    begin
      Prop.SetValue(myControl, GetDescription());
      Exit;
    end;
    Prop := Typ.GetProperty('Lines');
    if (Prop <> nil) and (Prop.Visibility = mvPublished) then
    begin
      (Prop.GetValue(myControl).AsObject() as TStrings).Add(GetDescription());
      Exit;
    end;
    ...
  finally
    Ctx.Free;
  end;
end;

 

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

×