Jump to content
JohnLM

Is it possible to copy all properties from one TMemo to a dynamically created TMemo?

Recommended Posts

Specs:  XE7, VCL, Win7 

 

I have a Panel with a TabControl in it and a Memo inside the first TabSheet on a Form that was all created and configured during design time.  And, on the Form in another Panel, I have a [Add] button to dynamically create a new TabSheet and Memo. 


The main Memo is the default with all the settings that I would like the dynamically created Memos to have.  But setting up all the necessary properties would be tedious and if I change something in the main Memo at design-time, I may not remember to update that to the dynamic ones.   

 

var
  Form1: TForm1;
  idx: integer=0; // index numb, for adding new tabs to the pagecontrol.

procedure TForm1.btnAddClick(Sender: TObject);
var
  TabSheet: TTabSheet;
  memo: TMemo;
begin
  inc(idx);
  TabSheet := TTabSheet.Create(PageControl1);
  TabSheet.Caption := 'Untitled-'+idx.ToString;
  TabSheet.PageControl := PageControl1;

  memo := tmemo.Create(pagecontrol1);
  // settings section
  memo.Color := $0017110D;
  memo.Align := alClient;
  .
  .
  .
  // end of settings section
  memo.Parent := tabsheet;
  memo.Show;
  memo.SetFocus;
end;

note: I have a lot more properties than what is posted above. 


Is there a better way to copy all the properties of the main Memo to the dynamically created ones and how can I do this?
 

Edited by JohnLM
updated missing code fragment

Share this post


Link to post
1 hour ago, JohnLM said:

if I change something in the main Memo at design-time, I may not remember to update that to the dynamic ones.

So don't set them at design-time; Set them at run-time in a function that will then also be used setup the other memos.

 

There aren't that many properties on a TMemo so I really don't think it's worth it to try and be clever here but if you really want to you can either use RTTI or streaming (see TStream.WriteComponent/ReadComponent). If your memo use event handlers you will get into trouble with the stream method.

Share this post


Link to post

Keep it simple - create a class helper for TMemo and add an Assign method and just set the props you need. This will be quicker and easier than messing with RTTI (which can be a bit of a rabbit hole with some property types).

  • Like 1

Share this post


Link to post
19 hours ago, Vincent Parrett said:

Keep it simple - create a class helper for TMemo and add an Assign method and just set the props you need. This will be quicker and easier than messing with RTTI (which can be a bit of a rabbit hole with some property types).

This is the way.

 

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

×