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.
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.
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.
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;
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?
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
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
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.