Solved

Destorying TMemo's

Posted on 2002-04-22
8
149 Views
Last Modified: 2010-04-04
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;

0
Comment
Question by:Tech_ie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 4

Expert Comment

by:nestorua
ID: 6960861
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.
0
 
LVL 2

Expert Comment

by:Kalroth
ID: 6960986
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 --
0
 

Expert Comment

by:ajvdvegt
ID: 6961070
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;
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:Kalroth
ID: 6961197
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 :)
0
 

Expert Comment

by:ajvdvegt
ID: 6961221
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.
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 200 total points
ID: 6961992
if you create a memo with

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


then the owner is the mainform,
if the mainform is about to free itself,
it frees automatically all owned objects,
you've nothing to do else

if you want to free the memo on buttonclick,
then you've first to fine the object, this can be done like

for i := 0 to mainform.componentcount - 1 do
  if (mainform.components[i] is tmemo) then
    tmemo(mainform.components[i]).free;

simple or?

to kalroth,
its recommended to post comments rather than answers

meikl ;-)
0
 

Author Comment

by:Tech_ie
ID: 6964097
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.
0
 

Author Comment

by:Tech_ie
ID: 6964102
Simple code yet very effective!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question