Recent

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

wp

  • Hero Member
  • *****
  • Posts: 12288
Re: Impossible to close a modal window...
« Reply #15 on: July 03, 2024, 08:11:18 pm »
Find in the attachment a small project which shows how to close a modal form by setting its ModalResult to mrClose (or anything else, except for mrNone).

MarkMLl

  • Hero Member
  • *****
  • Posts: 7483
Re: Impossible to close a modal window...
« Reply #16 on: July 03, 2024, 09:28:17 pm »
The problem is that I can't close the non-modal window so that the Main form reappears.

Reappears? What are you doing to make it disappear: a modal window/form would normally simply appear in front of the original one and be assigned the focus.

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

egsuh

  • Hero Member
  • *****
  • Posts: 1436
Re: Impossible to close a modal window...
« Reply #17 on: July 04, 2024, 07:54:43 am »
I believe wp's example will show the structure very well. I just want to add some more explanations, hoping that it would be easier for you to understand.
When you want to display another form (let's say Form2) from original form (let's say Form1), you can use

         Form2.Show;

This will show Form2. But you can go back to Form1 while Form2 is floating.

And if you call

         Form2.ShowModal;

then you have to close this form in order to go back to your original form (Form1).

In order to close a modal form, you can call Close or set the form's ModalResult property to any value of TModalResult type.
There are several TModalResults values like mrNone, mrAll, mrCancel, mrOK, mrAbort, mrRetry, mrIgnore, mrYes, mrNo.
So you can close shown-modal form in one of following ways.  But if you assign ModalResult := mrNone then the form will not be closed.

Code: Pascal  [Select][+][-]
  1. procedure TForm2.Button1Click(Sender: TObject);
  2. begin
  3.       Close;  
  4.      // or
  5.       ModalResult := mrOK;  // or mrCancel,  mrNo, mrYes, etc.  but mrNone will not close the form.
  6. end;
  7.  

Now, in the calling program, you will need the content in the modal form (form2) or not. So, if you have ALREADY CREATED form2, then following will work (and form2 is still stay in memory.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.       if Form2.ShowModal = mrOK then begin
  4.          Edit1.Text := Form2.Edit1.Text;   // here, first Edit1 is Self.Edit1, which is in the form.
  5.          Edit2.Text := Form2.Edit2.Text;   // Edit2,
  6.          AnyField := Form2.AField;   // Anyfield as well.
  7.      end;
  8. end;

In the above example, the key is :

      if Form2.ShowModal = mrOK

Which will be called when form2 returns modalresult, i.e. when the form is closed.  The form is CLOSED but still ALIVE in memory.
The codes within the if block will be executed only when the modal form (form2) was closed with mrOK, but will not be executed if form was closed with modal results of mrCancel, mrYes, etc. If you have put two buttons on TForm2, one with OK and another with Cancel, then you will want update form1's content when the user clicked OK button but not Cancel button. To do that ModalResult will be assigned different values depending on the clicked button.

Here, you can set it up in a few different ways.

You may define different event handler for the two buttons:

procedure TForm2.Button1Click(Sender);
procedure TForm2.Button2Click(Sender);


or,  you may use one single event handler for both buttons.

procedure TForm2.Button1Click(Sender);
begin
      if Sender = Button1 then ModalResult := mrOK
     else if Sender = Button2 then  ModalResult := mrCancel;
end;


Or you can set
       ModalResult property
of buttons directly at the object inspector at design time.

And if you close Form2 by calling Close, not assigning ModalResult, then the codes will not be executed.


There are cases when you do not want to pre-create form2. You may want to create it only when it is necessary.
You'd better get familiar with following structure.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. begin
  3.     with TForm2.Create (nil) do begin  // an instance is created here,
  4.         try
  5.             if ShowModal = mrOK then begin
  6.                 Self.Edit1.Text := Edit1.Text;           // First Edit1 needs Self. Without self, it will be regarded as TForm2's Edit1, as the second Edit1,
  7.                 Self.Edit2.Text := Edit2.Text;           // because this code is within with-block .
  8.                 Self.AnyField := AField;
  9.             end;
  10.        finally
  11.             Free;   // and the instance is freed here.
  12.        end;
  13.    end;
  14. end;

Well, these are what I had most difficulty when I first tried to use modal forms. Good Luck.
« Last Edit: July 04, 2024, 09:10:03 am by egsuh »

Zvoni

  • Hero Member
  • *****
  • Posts: 2620
Re: Impossible to close a modal window...
« Reply #18 on: July 04, 2024, 08:34:33 am »
Right, here's my take with my approach.
Reason for my approach: A Form is, in a nutshell, a class like any other you use throughout your program.

And there is one iron rule: A child never updates its parent.

The Parent creates the child and sends it to play
The child does its stuff
When done, the child returns to its parent, offering whatever results are expected.
The parent sends the child to bed after looking at the results
« Last Edit: July 04, 2024, 08:55:44 am 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

af0815

  • Hero Member
  • *****
  • Posts: 1356
Re: Impossible to close a modal window...
« Reply #19 on: July 04, 2024, 08:41:39 am »
And there is one iron rule: A child never updates its parent.
a little extension, if the child have to communicate with the parent, do it with callbacks (events), if a simple result is not enough.

For me, the same rules are for my Frames.
regards
Andreas

cdbc

  • Hero Member
  • *****
  • Posts: 1497
    • http://www.cdbc.dk
Re: Impossible to close a modal window...
« Reply #20 on: July 04, 2024, 08:54:37 am »
Hi
This:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. begin
  3.     with TForm2.Create (nil) do begin  // an instance is created here,
  4.         try
  5.             if ShowModal = mrOK then begin
  6.                 Self.Edit1 := Edit1;           // First Edit1 needs Self. Without self, it will be regarded as TForm2's Edit1, as the second Edit1,
  7.                 Self.Edit2 := Edit2;           // because this code is within with-block .
  8.                 Self.AnyField := AField;
  9.             end;
  10.        finally
  11.             Free;   // and the instance is freed here.
  12.        end;
  13.    end;
  14. end;
Will blow up in your face!
How the H*** can you point perfectly good edits to another set of edits, that are going to be free'd in the next line of code?!? Eaten some magic mushrooms lately, have you?!?
DO NOT DO THAT!
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Zvoni

  • Hero Member
  • *****
  • Posts: 2620
Re: Impossible to close a modal window...
« Reply #21 on: July 04, 2024, 08:57:56 am »
And there is one iron rule: A child never updates its parent.
a little extension, if the child have to communicate with the parent, do it with callbacks (events), if a simple result is not enough.

For me, the same rules are for my Frames.
IMO, this only applies if the childForm is still active/visible AND it's NOT a Modal Form

Remember: Showing a ChildForm in Modal state STOPS code-Execution in the Parent.
With a Callback-Event you would force code-execution in the Parent, which will probably blow up.
I'm not even sure that this could work
« Last Edit: July 04, 2024, 09:00:18 am 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

rvk

  • Hero Member
  • *****
  • Posts: 6324
Re: Impossible to close a modal window...
« Reply #22 on: July 04, 2024, 08:59:05 am »
Will blow up in your face!
How the H*** can you point perfectly good edits to another set of edits, that are going to be free'd in the next line of code?!? Eaten some magic mushrooms lately, have you?!?
What was probably meant was something like
Self.Edit1.Text := EditUsername.Text;
etc.

wp

  • Hero Member
  • *****
  • Posts: 12288
Re: Impossible to close a modal window...
« Reply #23 on: July 04, 2024, 09:07:23 am »
Hi
This:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. begin
  3.     with TForm2.Create (nil) do begin  // an instance is created here,
  4.         try
  5.             if ShowModal = mrOK then begin
  6.                 Self.Edit1 := Edit1;           // First Edit1 needs Self. Without self, it will be regarded as TForm2's Edit1, as the second Edit1,
  7.                 Self.Edit2 := Edit2;           // because this code is within with-block .
  8.                 Self.AnyField := AField;
  9.             end;
  10.        finally
  11.             Free;   // and the instance is freed here.
  12.        end;
  13.    end;
  14. end;
Will blow up in your face!
How the H*** can you point perfectly good edits to another set of edits, that are going to be free'd in the next line of code?!? Eaten some magic mushrooms lately, have you?!?
DO NOT DO THAT!
Regards Benny
You're planning to be the next grumpy man in this forum? Are such harsh words really needed to indicate a typo (Self.Edit1 := Edit1 rather than Self.Edit1.Text := Edit1.Text)?

What I don't like in egsuh's code, however, is the use of the with instruction - it makes it unnecessarily hard to tell to which "Edit1" refers.

egsuh

  • Hero Member
  • *****
  • Posts: 1436
Re: Impossible to close a modal window...
« Reply #24 on: July 04, 2024, 09:17:19 am »
Quote
How the H*** can you point perfectly good edits to another set of edits, that are going to be free'd in the next line of code?!? Eaten some magic mushrooms lately, have you?!?
DO NOT DO THAT!

You are right.

Quote
What I don't like in egsuh's code, however, is the use of the with instruction - it makes it unnecessarily hard to tell to which "Edit1" refers.

In practice, it's more likely to write like   (form1.)UserName := (form2.)edName.Text;, so not much confusion in most cases. But once I made mistake with "with", and want to underline the scope issue.

Zvoni

  • Hero Member
  • *****
  • Posts: 2620
Re: Impossible to close a modal window...
« Reply #25 on: July 04, 2024, 09:44:34 am »
But once I made mistake with "with", and want to underline the scope issue.
Easy solution: Don't use "with"
Have never used it in over 20 years of programming (Visual Basic, FPC)
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

MarkMLl

  • Hero Member
  • *****
  • Posts: 7483
Re: Impossible to close a modal window...
« Reply #26 on: July 04, 2024, 09:55:49 am »
But once I made mistake with "with", and want to underline the scope issue.
Easy solution: Don't use "with"
Have never used it in over 20 years of programming (Visual Basic, FPC)

I have, and intend to continue to. However there are two situations where I consider it to be bad news:

a) When nested.

b) As here: with ... create ... do ... free ... end;

Much has been written in the past by Sven and others drawing attention to other unfortunate cases. But I consider (b) above to be particularly pernicious, since not only does it risk generating unexpected code but it does not leave a (local) variable in a state where it can be inspected by the debugger.

An inline declaration along the lines of with a: sometype= b do, with a limited-scope local variable a acting as a shortcut in lieu of a more complex expression, would solve many of the problems but has been vetoed by the core language team.

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

pjtuloup

  • New Member
  • *
  • Posts: 28
Re: Impossible to close a modal window...
« Reply #27 on: July 04, 2024, 09:59:37 am »
Really, thank you all, thank you very much! I believe that the whole problem comes from the order in which the forms are created in the project options, and this bad order results from a bad arrangement of my procedures: because markMLI's remark is very correct, my main form should appear and it doesn't appear because my login form appears before.
I'll do all this again after a better analysis and carefully reading all your messages (thanks for your time).
I'll keep you informed.

pjtuloup

  • New Member
  • *
  • Posts: 28
Re: Impossible to close a modal window...
« Reply #28 on: July 10, 2024, 10:56:45 am »
Hi all,
I'm sorry but I tried to transpose the code kindly provided into Modal.zip and it doesn't work. I'm not saying that the code is wrong but it must not be compatible with my logic and I don't master Lazarus to find out why. Too bad, I give up, thank you all!

Thaddy

  • Hero Member
  • *****
  • Posts: 15509
  • Censorship about opinions does not belong here.
Re: Impossible to close a modal window...
« Reply #29 on: July 10, 2024, 11:09:19 am »
What is so difficult to understand you can only close a modal form from the modal form itself without hacks?
(I know I am repeating myself )
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.

 

TinyPortal © 2005-2018