Solved

Destorying TMemo's

Posted on 2002-04-22
8
143 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
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 4

Expert Comment

by:nestorua
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 2

Expert Comment

by:Kalroth
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Expert Comment

by:ajvdvegt
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Simple code yet very effective!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now