开发者

"form already exists" after form is closed and another instance opened. Delphi

I keep running into this strange problem. It almost seems to be random but I run my application and open a form, do some work with it and close it. The next time I go to open another isntance of that form I get an error messag开发者_JAVA百科e about the form already existing. This is very odd and I haven't been able to constantly reproduce the error.

If it helps I'm using Delphi 6 still. Is there some known reason why this is happening or what I can do to prevent it?


Are you sure the form is not being hidden when it is closed?

That is the default for MDI forms, but I have seen other people do the same (to speed up re-showing the form).

Edit (thanks Cosmin Prund for the comment!):

Hook the OnClose event of your form, and look what the value of the CloseAction parameter is. If it is caHide, then the form is hidden.

A temporary hack might be to assign caFree to the CloseAction, but a better solution is to watch the stack in your OnClose event handler to see how you ended up there, and what is causing the CloseAction to be caHide in the first place.

Note: in these situations you often want to see what the VCL does. So it is wise to enable the debug DCUs for your project; see this blog article how to do that (search for debug DCUs in the link).

--jeroen


You haven't provided the code, but it seems you are giving both form instances the same component name, and the owner of both forms is the same (probably Application object).

You cannot have components with the same name owned by another component. You should either give different names to each form instance, or just don't give any value to Name property, and let RTL choose a unique component name for your newly created instances.

If this is not the case with you, please provide the code by which you create your form instances, so that we can check what else might be wrong with the form.


 function ShowOnce( AFormClass:TFormClass;AShowing:Boolean=True):TForm;
 var 
   i : integer;
 begin
   Result := nil;

   for i := 0 to Screen.FormCount -1 do
     if Screen.Forms[i] is AFormClass then Result := Screen.Forms[i] as TForm;


   if not assigned(Result) then
     Result := AFormClass.Create(Application.MainForm); // Application or a parameter
   if Showing then
      Result.Show;
 end;

sample 1:

ShowOnce(TForm3); 

sample 2:

Form3:=ShowOnce(TForm3,False) as TForm3;
Form3.SomeProperty:=32;
Form3.Show;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜