Link to home
Start Free TrialLog in
Avatar of Tech_ie
Tech_ie

asked on

Destorying TMemo's

In my code I need to create several memo boxes. This is the easy part I just use the code 'With Tmemo.create(Self) do' code and create as many memo boxes as I need on the form.
But my Question is, How do I destory all the memo boxes created, by the click of a button?


{Creation Code}
 With TMemo.Create(Self) do      
      Begin
      Parent := MainForm;  
      Width := 200;
      Height := 200;
      End;

Avatar of nestorua
nestorua

HI, Tech ie,
If you have only the Memos you created on the Mainform and
need to destroy all of them then use
ComponentCount and Components property of your MainForm.
But if you have some memos on your mainform you don't want to destroy, you better add the names to your created memos
in order to distinguish them from those not to destroy.
Sincerely,
Nestorua.
This probably isn't what most coders would consider nice/clean/proper code, but it'll get the job done.
Using this "technique" you won't have to mess with .Components or the component counter. You HAVE to free them yourself though, since their owner is nil.
If I had more time, then I'd probably write an object that manages a TList for the object handling :-)

I hope this code is fairly straight forward and/or answers your question.

-- SNIPIT CODE --

var
  myMemos : Array of TMemo;

procedure TmainForm.addMemoButtonClick(Sender: TObject);
begin
  SetLength(myMemos,High(myMemos)+2);

  myMemos[High(myMemos)] := TMemo.Create(nil);
  with myMemos[High(myMemos)] do begin
    Top := Random(400);
    Left := Random(400);
    Width := 100;
    Height := 100;
    Parent := mainForm;
  end;
end;

procedure TmainForm.removeALLMemosButtonClick(Sender: TObject);
var
  memoCount : Integer;
begin
  for memoCount := Low(myMemos) to High(myMemos) do begin
    myMemos[memoCount].Free;
    myMemos[memoCount] := nil;
  end;
  SetLength(myMemos,0);
end;

-- SNIPIT CODE --
Instead fo using a array to store the TMemo's, you could also use a TObjectList. If you make it own its children (default behaviour), then they will be free'd when you free the objectlist - less typing :)

Sample code (from my head, so function names can differ slightly):

var
  Memolist: TObjectlist;

procedure TMainForm.OnCreate(Sender: TObject);
begin
  Memolist := TObjectlist.create;
end;

procedure TMainForm.OnDestroy(Sender: TObject);
begin
  Memolist.free;
end;

procedure TMainForm.addMemoButtonClick(Sender: TObject);
var
  Memo: TMemo;
begin
  Memo := TMemo.Create(nil);
  with Memo do begin
    Top := Random(400);
    Left := Random(400);
    Width := 100;
    Height := 100;
    Parent := mainForm;
  end;
  Memolist.add(memo);
end;
I bow to the master :)

That solution isn't only easier, but also smarter. I guess I should think more carefully before I post next time.

One minor thing though, on the create of the TObjectList you need to specify OwnsObjects as; Memolist := TObjectList.Create(True); But apart from that, a much better solution, points should be awarded to you :)
Wow, first post here and such rewards all of a sudden! Nice :)

You're right about OwnsObjects:=true, but if you omit it with Create, it defaults to True (at least, with Delphi 5). For clearity however, it might indeed be better to add (true) to Create, so you see you can select something there when reviewing your code later.
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tech_ie

ASKER

I have accepted kretzschmar's answer. It runs lovely in my program. It's really simple and I should have known it myself. Thanks All for helping.
Avatar of Tech_ie

ASKER

Simple code yet very effective!