Recent

Author Topic: SOLVED Impossible to close a modal window...  (Read 3191 times)

pjtuloup

  • New Member
  • *
  • Posts: 28
SOLVED Impossible to close a modal window...
« on: July 03, 2024, 09:43:50 am »
Hello everyone... New beginner problem: I can't close a modal window.
Here's how things look: My Main form (Master) calls in its FormActivate procedure a modal window used for identification.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormActivate(Sender: TObject);
  2. begin
  3.   FormIdentif.ShowModal;
  4. end;  

In this FormIdentif form there is a "Continue" button which should close the window so that the Master file is accessible with its general menu

Code: Pascal  [Select][+][-]
  1. procedure TFormIdentif.Btn_ContinuerClick(Sender: TObject);
  2. begin
  3.   //Close;
  4.   //FormIdentif.Close;
  5.   //FormIdentif.ModalResult:= 1;
  6.   //FormIdentif.ModalResult:= mrOK;
  7. end;
  8.  

I tried several things which don't work: either nothing happens (using ModalResult) or the application closes (with Close)

I don't see what I'm missing... Can anyone point me in the right direction? Thanks in advance!
« Last Edit: July 10, 2024, 11:12:10 am by pjtuloup »

Thaddy

  • Hero Member
  • *****
  • Posts: 15487
  • Censorship about opinions does not belong here.
Re: Impossible to close a modal window...
« Reply #1 on: July 03, 2024, 09:48:57 am »
You can't easily close a modal window from somewhere else. You close a modal Window from the modal window itself. That is by design. That does not mean it can not be done, but it should not be done.
It is a bit useless to show you how to do it since you are a beginner.
I will not show you bad practice, but someone else on this forum with less understanding of the matter will show you how to do it anyway... :o :D and he or she can count on my anger... The proverbial >:D

The easy way out for a beginner is to make the window NOT modal.
« Last Edit: July 03, 2024, 10:00:12 am by Thaddy »
My great hero has found the key to the highway. Rest in peace John Mayall.
Playing: "Broken Wings" in your honour. As well as taking out some mouth organs.

pjtuloup

  • New Member
  • *
  • Posts: 28
Re: Impossible to close a modal window...
« Reply #2 on: July 03, 2024, 09:55:06 am »
Thanks but A clarification: the procedure TFormIdentif.Btn_ContinuerClick is in the form of the modal window I want to close.

MarkMLl

  • Hero Member
  • *****
  • Posts: 7419
Re: Impossible to close a modal window...
« Reply #3 on: July 03, 2024, 09:58:18 am »
In this FormIdentif form there is a "Continue" button which should close the window so that the Master file is accessible with its general menu

Make sure that the button you've labelled "Continue" is in the context of the modal form, and is marked as having e.g. a cancel effect. Can't give a more detailed example at the moment, but would echo (perhaps more gently) Thaddy's warning that this is something that shouldn't be done from outside the form.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Zvoni

  • Hero Member
  • *****
  • Posts: 2607
Re: Impossible to close a modal window...
« Reply #4 on: July 03, 2024, 10:07:40 am »
I'm probably going to invoke Thaddy's Anger, but hey...no risk no fun :D

The way i do it (No code, just "steps")

1) In your Main Form you have a var for your ModalForm
2) In your MainForm you create the ModalForm, then you ShowModal that Form --> Code-Execution in your MainForm is "stopped" as long as your ModalForm is visible (here Line 3)
Code: Pascal  [Select][+][-]
  1. //In TMainForm
  2. ModalForm:=TModalForm.Create;
  3. ModalForm.ShowModal;
  4. ModalForm.Close;
  5. ModalForm.Free;
  6.  
3) In your ModalForm you have a Button (or whatever else) to close that ModalForm. I do
Code: Pascal  [Select][+][-]
  1. procedure TModalForm.cmdCloseClick(Sender: TObject);
  2. Begin
  3.    Self.Close;
  4. End;
  5. procedure TModalForm.FormClose(Sender: TObject;
  6.   var CloseAction: TCloseAction);
  7. begin
  8.   CloseAction:=caHide;  //THIS ONE
  9. end;    
After executing above statement, code-execution returns to your MainForm to Line 4 above

...and now i have to run and hide, so Thaddy doesn't kill me... :D
« Last Edit: July 03, 2024, 03:15:06 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

n7800

  • Full Member
  • ***
  • Posts: 134
Re: Impossible to close a modal window...
« Reply #5 on: July 03, 2024, 10:18:34 am »
I just checked and Close or ModalResult should work.

But the code in the OnActivate event always bothers me. It can cause problems.

For example, if you write Show instead of ShowModal in your program, the window will close, focus will switch to the first form, which will activate the OnActivate code, and it will open the second form again. This will happen very quickly and will look like it never closed.

That's the first thing I thought of when I saw the event. However, for modal forms this does not cause a loop and the form closes...

pjtuloup

  • New Member
  • *
  • Posts: 28
Re: Impossible to close a modal window...
« Reply #6 on: July 03, 2024, 10:21:45 am »
Thank you very much for the advice... and taking risks. I will study this.

I have to say that I am grateful for the warnings since you are experienced, but I don't understand why you say that I am trying to close a modal window from outside the form, since my "continue" button that wants to close the form is indeed in this form?

rvk

  • Hero Member
  • *****
  • Posts: 6288
Re: Impossible to close a modal window...
« Reply #7 on: July 03, 2024, 10:22:43 am »
You can't easily close a modal window from somewhere else. You close a modal Window from the modal window itself.
Have I read the question wrong... or did you???

TS says (s)he want's to close the modal form IN THE MODAL FORM itself via a button TFormIdentif.Btn_ContinuerClick.

The only problem I see with the code is that it calls the instance variable itself "FormIdentif" and not using "Self." or just using ModalResult directly (without FormIdentif in front).

ModalResult:= mrOK; should have been sufficient in that button-event on the modal form.

Edit:
I have to say that I am grateful for the warnings since you are experienced, but I don't understand why you say that I am trying to close a modal window from outside the form, since my "continue" button that wants to close the form is indeed in this form?
Yes. Exactly. Ignore grumpy Thaddy. He doesn't always read the questions carefully.

MarkMLl

  • Hero Member
  • *****
  • Posts: 7419
Re: Impossible to close a modal window...
« Reply #8 on: July 03, 2024, 10:30:40 am »
Thank you very much for the advice... and taking risks. I will study this.

I have to say that I am grateful for the warnings since you are experienced, but I don't understand why you say that I am trying to close a modal window from outside the form, since my "continue" button that wants to close the form is indeed in this form?

I was being cautious after Thaddy's interpretation :-/

Apart from that: you don't really need to close the form as such, just exit it using the appropriate button.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 15487
  • Censorship about opinions does not belong here.
Re: Impossible to close a modal window...
« Reply #9 on: July 03, 2024, 10:33:33 am »
Rik, my mother taught me speed reading when I was 5 but never mentioned the disadvantages..So I ignore them by habit  :(
My great hero has found the key to the highway. Rest in peace John Mayall.
Playing: "Broken Wings" in your honour. As well as taking out some mouth organs.

MarkMLl

  • Hero Member
  • *****
  • Posts: 7419
Re: Impossible to close a modal window...
« Reply #10 on: July 03, 2024, 10:42:55 am »
Rik, my mother taught me speed reading when I was 5 but never mentioned the disadvantages..So I ignore them by habit  :(

Thaddy, I'm very sorry to have to say this but we've noticed.

You really should be a bit more careful particularly when- as you've noted yourself- the person asking the question is a newcomer to the language, IDE or forum.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

dseligo

  • Hero Member
  • *****
  • Posts: 1331
Re: Impossible to close a modal window...
« Reply #11 on: July 03, 2024, 11:43:31 am »
In this FormIdentif form there is a "Continue" button which should close the window so that the Master file is accessible with its general menu

It should work just with Close;.

Check if your method is actually called when you press 'Continue' button:
Code: Pascal  [Select][+][-]
  1. procedure TFormIdentif.Btn_ContinuerClick(Sender: TObject);
  2. begin
  3.   ShowMessage('works!');
  4.   Close;
  5.   //FormIdentif.Close;
  6.   //FormIdentif.ModalResult:= 1;
  7.   //FormIdentif.ModalResult:= mrOK;
  8. end;
  9.  

Other thing you could check is if you use 'OnClose' event, and in there set 'CloseAction' parameter to caNone.

If nothing works, I suggest you make test project that shows problem and post it here.

rvk

  • Hero Member
  • *****
  • Posts: 6288
Re: Impossible to close a modal window...
« Reply #12 on: July 03, 2024, 12:02:16 pm »
Close; and ModalResult := mrOk; should both work fine.

(Note the absense of the "FormIdentif." in front of the ModalResult !! )

But be careful, you have the FormIdentif.ShowModal in your Form1.OnActivate.

Why did you put it there?
The OnActivate can be triggered multiple times during the lifetime of the TForm (after each Form1.Show).

Did you perhaps want it in OnCreate (where it's guaranteed to only show once, during creation of your Form1) ?

https://wiki.lazarus.freepascal.org/Event_order#Forms

pjtuloup

  • New Member
  • *
  • Posts: 28
Re: Impossible to close a modal window...
« Reply #13 on: July 03, 2024, 07:49:12 pm »
Nothing works from everything I've tried. Also, I abandoned the idea of ​​making a modal window.
The problem is that I can't close the non-modal window so that the Main form reappears.
I agree that run a secondary window in the MainFormActivate() was not a good idea. But it's not better with the MainFormShow().

Here's what I did or tried to do next:
I thought that the secondary window (FormIdentif) should only be launched if the user identification has not yet been done and I created the global variable IdentifOK to store the information.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormShow(Sender: TObject);
  2. begin
  3. if IdentifOK=false then
  4.    begin
  5.    FormIdentif.Show;
  6.    end
  7. end;
  8.  

And clicking on the famous "continue" button now launches the following actions

Code: Pascal  [Select][+][-]
  1. procedure TFormIdentif.Btn_ContinuerClick(Sender: TObject);
  2. begin
  3.   //Close
  4.   //Hide;
  5.   Application.MainForm.Show;
  6. end;    
  7.  
  8. Nota: elsewhere in the code IdentifOK= true if the identification was successful
  9.  
  10. But all this doesn't work either: when I click on the "continue button", the MainForm does not appear and the procedure actually loops.
  11.  
  12. I spent the day on this, I'm tired, I'm going to sleep...
  13.  

rvk

  • Hero Member
  • *****
  • Posts: 6288
Re: Impossible to close a modal window...
« Reply #14 on: July 03, 2024, 08:05:11 pm »
Nothing works from everything I've tried.
Also, I abandoned the idea of making a modal window.
The problem is that I can't close the non-modal window so that the Main form reappears.
We showed you how it would work.
You can make a new small project with main form and secondary form and just try it with that. It'll probably work. Then you can find out why it doesn't in your big project.

I agree that run a secondary window in the MainFormActivate() was not a good idea. But it's not better with the MainFormShow().
As I already said... I worked put it in the OnCreate (TForm1.FormCreate). Not OnShow.
The main form is not visible yet but it will show the secondary form as modal.
Sort of a splash or login form. (was that what you were going for?)

Here's what I did or tried to do next:
I thought that the secondary window (FormIdentif) should only be launched if the user identification has not yet been done and I created the global variable IdentifOK to store the information.
So it's a login form.
I would definitely use a showmodal for that.
Or block the main menu ir action until you are sure the credentials are correct.

But all this doesn't work either: when I click on the "continue button", the MainForm does not appear and the procedure actually loops.
Yep. In the login you call mainform.show which triggers the event again.

I think your first draft with showmodal should have worked but I think you have something in your code which gives you problems. That's why it's best to create a small sample project to show it should work.

BTW. Isn't there a special login component which you can use (with username and password). Or do you have other special things on you login form?

Good night 😴

 

TinyPortal © 2005-2018