Solved

Destorying TMemo's

Posted on 2002-04-22
8
148 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Downloading email attachments 2 96
Why can't I create any project with my delphi EXE file? 6 59
Delphi: making a BW image transparent 10 138
Twebbrowser add css to the header 3 54
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

752 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