Solved

Destorying TMemo's

Posted on 2002-04-22
8
145 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
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

786 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