Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MDI Children

Status
Not open for further replies.

vacunita

Programmer
Aug 2, 2001
9,166
MX
does anybody know how to Destroy, Close, Free, or release a MDI child, every time I try do destroy one i get an access violation.Any help would be appreciated.

thanx
 
In relation to this question, hoe do you access components of a dinamically created MDIchild From. Since i created the MDIchild from a base form that had a TMemo. i need to write text into it from a file but cant seem to be able to access the memo in the newly created form. It's there it's created with the form it visible enabled and i can write text into it at run rime, but i cant access it programatically. Any help would be appreciated.

Thanx.
 
To release an MDI child you can make an OnClose handler and set the action to caFree. (Example from Delphi help:)

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);

begin
if MessageDlg('Close application ?', mtConfirmation,
[mbYes, mbNo], 0) = mrYes then
Action := caFree
else
Action := caNone;
end;


Then if you want to free the form programmatically you can just call form.close.

To access the components on a dynamically created MDI form you need to keep a variable pointing to the form itself. Since you can probably create multiple instances of the form you might want to keep a list.

Hope this helps!

TealWren
 

This is how I open a file into a MDI child, in the main code
(simplified)

with TEditor.create(self) do
begin
Open(ReqdFile);
end

The procedure Open is part of the Child form code.
codeeditor is a richedit (could be a memo)

procedure TEditor.Open(const FileName);
begin
PathName := FileName;
with codeeditor do
try
Lines.LoadFromFile(PathName);
//fix the richedit large file bug
maxlength := 2000000;
Readonly := false;
Caption := ExtractFileName(PathName);
except
showmessage('Handle error');
end;
end;



I have alway had a problem closing an application from a Child window. is this one of your problems ?

You must close the chidren from the main form, so a child window exit menu item has to call the main form procedure,
to close the child windows, but you cant do this as you are attempting to destroy the calling code.
I usualy fudge this by not allowing a child window access to program exit.

procedure TEditor.Exit1Click(Sender: TObject);
begin
// call main form exit proc
mainform.CloseEditorFiles(nil);
close;
// if we call this here we get a crash !!!!!!
end;

Steve..


 
If your OnClose sets the action to caFree, all you should need to do is call Close in the Exit1Click procedure. What does your mainForm.CloseEditorFiles() procedure do? If it attempts to free the MDI children that would be causing your access violation.
TealWren
 
either i'm no understanding all you, or i did no make my self clear in the original question. All i want to do is close a MDIchild. when the user clicks on the childs x button, or on the main form's menu on close or Close All. My app, creates the children dinamically:
procedure TMainForm.New1Click(Sender: TObject);
var memovent:TRichEdit;
begin
Veditors:=TForm.Create(self);
Veditors.Name:='editor'+IntToStr(editorsCount);
Veditors.FormStyle:=fsMDIChild;
Veditors.Parent:=Form1;
Veditors.Caption:='Editor '+IntToStr(editorsCount);
Veditors.Height:=300;
Veditors.width:=300;
memovent:=TRichEdit.Create(self);
memovent.parent:=Veditores;
memovent.Align:=alClient;
editorsCount:=editorsCount+1;
end;
this creates a form with a Richedit box in it. I have figured out how to open the file into the editbox, theres no real problem there. but now, the user has made the changes to the file he has saved the changes and he wants to close that editor, but not the program. I need to get rid of that child. but keep any other children that are opened.
Now how do i close it?

Thanx all of you for taking the time to help me.

Hope to here from you soon.
 
When you click the X in the corner, does your MDI child minimize instead of closing? TealWren
 
thats right. it goes down to the corner of the parent window.
 
All you need to do is this:
In your MDI child form, click events, doubleclick OnClose to create an OnClose handler. Add the code so it looks like the following:

procedure TMyForm.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
Action := caFree;
end;

Then, when you click the X the form will free instead of minimizing.
TealWren
 
Since my forms are created dinamically i cannto edit them in the object inspector. hence cannot acces there events. any other ideas?
 
Create a procedure in your Form1 class like this:

procedure TForm1.ChildFormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action := caFree;
end;

Then in your dynamic allocation, you can put
VEditors.OnClose=ChildFormClose;

That should do it. TealWren
 
Correction, it closes the MDI child, but returns an Access Violation.
 
What else are you doing? The access violation is most likely caused by a call to VEditors.free, or an attempt to access a member of VEditors after the close. TealWren
 
o.k, i got it, its working now. I just had to remove the part where i told the MDI child that its parent was Form1. after that it worked perfectly.

Thanx. Alot.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top