This is in the MDIMainForm
type
TFormList = record
Form: TForm;
PrimaryKey: Integer;
Type: String;
Then I have a FormList: Array of TFormList; and this all works fine. No
problems at all. It's possible to have unlimited amount copies of the same
form. For every form I create a new record and set the properties. But the
problem occurs when user has closed one of the forms and changes the focus
to another window in the application. I have the following code to check if
the form exists and if it does, locate record from ClientDataSet. This code
is called at Screen.OnActiveFormChange.
for I := 0 to Length(FormList) - 1 do
begin
if (FormList[I].Form.HandleAllocated) then
if (FormList[I].Form.Name = ActiveMDIChild.Name) then
begin
cds.Locate('PrimaryKey;Type', VarArrayOf([FormList[I].PrimaryKey,
FormList[I].Type]), []);
Break;
end;
end;
Sometimes this works, but usually I get AVs. My guess is that the AVs come
from "if (FormList[I].Form.Name", reason for this could be that the Form
isn't created. Or it was until user closed it and trying to access something
that doesn't exists anymore. Correct? Could there be other reasons for AVs?
Cheers,
Teemu
>Then I have a FormList: Array of TFormList; and this all works fine. No
>problems at all. It's possible to have unlimited amount copies of the same
>form. For every form I create a new record and set the properties. But the
>problem occurs when user has closed one of the forms and changes the focus
>to another window in the application. I have the following code to check if
>the form exists and if it does, locate record from ClientDataSet. This code
>is called at Screen.OnActiveFormChange.
>
>for I := 0 to Length(FormList) - 1 do
>begin
> if (FormList[I].Form.HandleAllocated) then
...
>Sometimes this works, but usually I get AVs. My guess is that the AVs come
>from "if (FormList[I].Form.Name", reason for this could be that the Form
>isn't created. Or it was until user closed it and trying to access something
>that doesn't exists anymore. Correct?
Probably - You create the recor when you create the child, but who
erases of cleans up the record when the child is destroyed? For that
matter, is the child being destroyed or simply closed?
One approach is to let the child own its own TFormList record. Then
the main form can do something like:
with ActiveMDIChild as TMyMDIChild do
cds.Locate('PrimaryKey;Type',
VarArrayOf(PrimaryKey, Type]),
[]);
where Type and PrimaryKey are fields of the MDI child form.
Otherwise someobdy is doing to have to notice when a child is
destroyed and clean up the list.
Good luck.
Kurt
If you do not have an OnClose event handler for the MDI children that sets
Action := caFree the forms will not be destroyed but only minimized when the
user tries to close them. If you do have such a handler then it would have to
take steps to remove the forms record from the FormList before the form is
actually destroyed.
> I have the following code to check if
> the form exists and if it does, locate record from ClientDataSet. This code
> is called at Screen.OnActiveFormChange.
>
> for I := 0 to Length(FormList) - 1 do
> begin
> if (FormList[I].Form.HandleAllocated) then
> if (FormList[I].Form.Name = ActiveMDIChild.Name) then
The problem is the activeMDICHild.Name statement. ActiveMDIChild can be Nil,
and if it is the statement causes an access violation. Have you tried to use
the MDI child forms OnActivate event to locate the matching record in the CDS?
For MDI children the event fires when the user switches between child forms.
--
Peter Below (TeamB)
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be
I have Action := caFree; and Self := Nil; OnClose event.
> > I have the following code to check if
> > the form exists and if it does, locate record from ClientDataSet. This
code
> > is called at Screen.OnActiveFormChange.
> >
> > for I := 0 to Length(FormList) - 1 do
> > begin
> > if (FormList[I].Form.HandleAllocated) then
> > if (FormList[I].Form.Name = ActiveMDIChild.Name) then
>
> The problem is the activeMDICHild.Name statement. ActiveMDIChild can be
Nil,
> and if it is the statement causes an access violation. Have you tried to
use
> the MDI child forms OnActivate event to locate the matching record in the
CDS?
> For MDI children the event fires when the user switches between child
forms.
I had ActiveMDIChild <> Nil check, but it didn't help, so it's not that.
Altho, it should have that check just be sure that it isn't nil. Yes and no
for the last question.
I did that, but changed my mind beacuse more forms that uses this technique
can be added to the project. I tought this would be "general" for all..
Saving code hehe :) Well, I'll try that again.
But the stranges thing (of course) is that before I TListView (with indexes)
to do the FormList[] picking, now I use DBGrid instead and get these AVs.
The ListView.ItemIndex was 1:1 to the FormList, which made things easier.
Now when the DBGrid dataset can be filtered, I have to locate the right form
from the records.
Sounds right. Add a bit of code to the .OnClose event that finds itself in
the array an removes itself. Also, you might want to consider using a TList
instead, as it's far more flexible and powerful than an array.
Cheers,
Ignacio
--
No, don't send me e-mail directly. No, just don't.
"Ignacio Vazquez" <ivazquezATorioncommunications.com> wrote in message
news:3f4b6a71$1...@newsgroups.borland.com...
Cheers,
Teemu
Cheers,
Teemu
Yes.
> If so, why isn't it nilled after the form has closed?
Because objects don't automagically nil out any references to them.
> I do check Form <>
> Nil before Form.Name but it doesn't trap into that... I
> also tried to set
> the Form := Nil but that causes AVs too (referencing).
What about removing the entry in the form's .OnClose event?
Self := nil accomplishes exactly nothing, self is a pass-by-value parameter,
so this will *not* in any way change the other form references around, e.g.
that in your Formlist. What you need to do is walk over the Formlist and find
the record for which formlist[i].Form = self. That is the record you need to
remove from the list.
Cheers,
Teemu
"Peter Below (TeamB)" <10011...@compuXXserve.com> wrote in message
news:VA.0000a31...@nomail.please...
I just thought that if I check the Handle of the form it would give me False
as result but..
Thank you Ignacio and others who helped me.
Cheers,
Teemu
"Ignacio Vazquez" <ivazquezATorioncommunications.com> wrote in message
news:3f4b79ba$1...@newsgroups.borland.com...