Recent

Author Topic: Closing Multi ShowModal Forms  (Read 12464 times)

vlado

  • Guest
Closing Multi ShowModal Forms
« on: August 27, 2012, 04:35:28 pm »
Hi all,

When showing in the main Form new Form (Form1) with ShowModal, then in the Form1 new form,  Form2, also with ShowModal, everything works as it should until the second ShowModal form is to be closed by OnKeyDown event.

Then, with one KeyDown (e.g. Key = 27) both forms (Form1 and Form2) are closed and main Form is Displayed, but waiting for ONE KeyDown! (~Press or ~Up) and after the key event has triggered then main Form have focus.

Shouldn't it be closing ONE form with ONE KeyDown? Is this a bug, because if I remember well that worked on Delphi?

Thanks in advance,
Vlado

(Using Lazarus v0.9.30.4 on Windows XP Home)
« Last Edit: August 28, 2012, 11:59:08 am by vlado »

ezlage

  • Guest
Re: Problem with Closing Multi ShowModal Forms
« Reply #1 on: August 27, 2012, 05:30:55 pm »
Are you doing this way?

Code: [Select]
FormX.Free;
FormX:=TFormX.Create(Application);
FormX.ShowModal;
« Last Edit: August 27, 2012, 05:43:20 pm by ezlage »

denver

  • Jr. Member
  • **
  • Posts: 67
Re: Problem with Closing Multi ShowModal Forms
« Reply #2 on: August 27, 2012, 05:34:13 pm »

Have you try to add  Key := 0 ; to clear the keyboard buffer ?

example :

Code: [Select]

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState  );
begin
  if Key = 27 then
    begin
    Key := 0 ;  // <-----  to Clear the Keyboard buffer
    Close ;
    end;
end;


vlado

  • Guest
Re: Problem with Closing Multi ShowModal Forms
« Reply #3 on: August 27, 2012, 05:56:25 pm »
@ezlage:
No, I'm doing it like this:

Project:
Code: [Select]
Application.CreateForm(Tfm_Main, fm_Main);
Application.CreateForm(Tfm_Form1, fm_Form1); Application.CreateForm(Tfm_Form2, fm_Form2);

Main:
Code: [Select]
fm_Form1.ShowModal;
fm_Form1.FormKeyDown:
Code: [Select]
If Key = 27 (and some other conditions) Then Close;
If Key = 121 Then fm_Form2.ShowModal;

fm_Form2.FormKeyDown:
Code: [Select]
If Key = 27 Then Close;
« Last Edit: August 27, 2012, 06:22:49 pm by vlado »

vlado

  • Guest
Re: Problem with Closing Multi ShowModal Forms
« Reply #4 on: August 27, 2012, 06:03:40 pm »
@denver:
Clearing the buffer (Key := 0) doesn't fix the problem...

ezlage

  • Guest
Re: Problem with Closing Multi ShowModal Forms
« Reply #5 on: August 27, 2012, 06:12:54 pm »
Try this way, please:

Project:
Code: [Select]
Application.CreateForm(Tfm_Main, fm_Main);
Application.CreateForm(Tfm_Form1, fm_Form1);
Application.CreateForm(Tfm_Form2, fm_Form2);

Main:
Code: [Select]
fm_Form1.Free;
fm_Form1:=Tfm_Form1.Create(Application);
fm_Form1.ShowModal;


Form1.OnKeyDown:
Code: [Select]
if Key=#27 {and ...} //#27=ESC
  then begin
    key:=0; //like denver said
    Close;
    //if you really want to reopen form after close it:
    fm_Form1.Free;
    fm_Form1:=Tfm_Form1.Create(Application);
    fm_Form1.ShowModal;
  end;

Form2.OnKeyDown:
Code: [Select]
if Key=#27 {and ...} //#27=ESC
  then begin
    key:=0; //like denver said
    Close;
    //if you really want to reopen form after close it:
    fm_Form2.Free;
    fm_Form2:=Tfm_Form1.Create(Application);
    fm_Form2.ShowModal;
  end;
« Last Edit: August 27, 2012, 06:15:50 pm by ezlage »

vlado

  • Guest
Re: Problem with Closing Multi ShowModal Forms
« Reply #6 on: August 27, 2012, 06:33:48 pm »
@denver: Done, but on Key=#27 (on Form2), error message appears:
Quote
uf_Form2.pas(59,10) Error: Incompatible types: got "Char" expected "LongWord"

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: Problem with Closing Multi ShowModal Forms
« Reply #7 on: August 27, 2012, 06:40:20 pm »
KeyDown event doesn't expect a Char,

Either use the OnKeyPress event , or use VK_ESCAPE instead of #27.
note:  VK_ constants are in lcltype unit.

ezlage

  • Guest
Re: Problem with Closing Multi ShowModal Forms
« Reply #8 on: August 27, 2012, 06:41:20 pm »
Sorry,

Use the OnKeyPress, not OnKeyDown. Like KpjComp said.

Regards.

vlado

  • Guest
Re: Problem with Closing Multi ShowModal Forms
« Reply #9 on: August 27, 2012, 07:07:06 pm »
Used OnKeyPress but to clear a buffer is it now:

Code: [Select]
Key:=#0
? 'Cause on:

Code: [Select]
Key := 0
there's compiler error :
Quote
uf_Form2.pas(67,12) Error: Incompatible types: got "ShortInt" expected "Char"

Anyway, I have used Key:=#0 and it doesn't work well! Now, after closing Form2 with ESC key, Main form is also displayed (Should be form1, right?) and nothing can be done: no focus and Main Form can't even be closed with ALT+F4!
Have to use Task Manager to close App... Uh, oh :-)
« Last Edit: August 27, 2012, 07:11:33 pm by vlado »

ezlage

  • Guest
Re: Problem with Closing Multi ShowModal Forms
« Reply #10 on: August 27, 2012, 07:10:06 pm »
The Close procedure doesn't quit application.
You need to use Application.Terminate after of Close or call MainForm.Show.

I think that in OnKeyPress isn't required to clean var key.

Sorry by my poor english.
« Last Edit: August 27, 2012, 07:16:24 pm by ezlage »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Problem with Closing Multi ShowModal Forms
« Reply #11 on: August 27, 2012, 07:13:45 pm »
Don't close the form then (although I think that this needs to be corrected) just use modalresult := mrcancel or mrnone to hide the form. Since the forms are auto created on start up this will help you avoid more errors later on too.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

vlado

  • Guest
Re: Closing Multi ShowModal Forms
« Reply #12 on: August 30, 2012, 02:33:38 pm »
Thank you all for opinions.

@taazz: In Delphi Applications with this code I didn't have any errors. What errors you think can occur using Form Close with Lazarus?

Now, it seems that it's working when I do it this way: From Main Form I call ShowModal Form1. Then, From Form1 I just call Show Form2 (not Modal!). When going back with Form Close, wanted behaviour, so far so good.

« Last Edit: August 30, 2012, 02:35:39 pm by vlado »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Closing Multi ShowModal Forms
« Reply #13 on: August 30, 2012, 02:58:22 pm »
Thank you all for opinions.

@taazz: In Delphi Applications with this code I didn't have any errors. What errors you think can occur using Form Close with Lazarus?

the same as with delphi when you close a form it is usually destroyed too so the next time a show or showmodal call appears with out first creating the form you get a GPF or SIGSEGV in FPC. The only way to avoid that is to write an onclose handler and set the CloseAction := caHide or create the form when ever is needed and destroy it after showModal
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

vlado

  • Guest
Re: Closing Multi ShowModal Forms
« Reply #14 on: August 30, 2012, 03:13:01 pm »
Quote
@taazz: In Delphi Applications with this code I didn't have any errors. What errors you think can occur using Form Close with Lazarus?

the same as with delphi when you close a form it is usually destroyed too so the next time a show or showmodal call appears with out first creating the form you get a GPF or SIGSEGV in FPC. The only way to avoid that is to write an onclose handler and set the CloseAction := caHide or create the form when ever is needed and destroy it after showModal

Hm, I'm not gettin' any error messages... (had a small test, called Form2 from Form1 100 times and closed it on ESC Keypress with Close: works without errors, at least on Windows XP)
« Last Edit: August 30, 2012, 03:15:40 pm by vlado »

 

TinyPortal © 2005-2018