Jump to content
Jud

Multiple similar components

Recommended Posts

I have 20 TMemos, named memo1 ... memo20.  I have an integer which indicates which memo is to be written to.  Is there a direct way to write to the desired memo, as opposed to a large, ugly case statement?

Share this post


Link to post

The most direct/efficient way is to put the Memo object pointers into an array or list, and then use the integer as an index into that container. 

Edited by Remy Lebeau

Share this post


Link to post

As Remy already mentioned an array or list works. I tend to put such things in a dynamic array. Can move the array to someplace higher in scope if desired but for code that doesn't run that often I usually create it as needed and let it fall out of scope and be destroyed.

procedure TForm1.Button1Click(Sender: TObject);
Var
  SomeMemos : array of TMemo;
begin
  SomeMemos := [Form1.Memo1,Form1.Memo2,Form1.Memo3,Form1.Memo4,Form1.Memo5];
  // Dynamic Arrays are zero based so need to subtract one compared to 1 based
  SomeMemos[5 -1].Lines.Add('test!');
end;

 

Edited by Brian Evans

Share this post


Link to post

Thanks, it works well.  You guys are the greatest!

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

×