Recent

Author Topic: [SOLVED] mrOK of MessageDlg in Form 2 also closes Form2.ShowModal  (Read 2349 times)

xint

  • New Member
  • *
  • Posts: 18
Hello,

After a long time, I have finally found the time and energy to work on an old project.
However, I've been stuck on the following problem for a while now, so I suspect I won't be able to solve it myself without help.

I have an application (Form1) with a form (Form2), which I call up as required.


The only problem is that confirming the dialog always closes Form2 too. It's as if the 'mrOK' is simply forwarded.
I didn't found anything online about this problem. So, where is my error?

Thanks !
Stefan

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

There is some interaction in said form, e.g.

Code: Pascal  [Select][+][-]
  1. procedure TForm2.BtnTestConnectionClick(Sender : TObject);
  2. var
  3.     pt : TPoint;
  4. begin
  5.     With ZConnection2 do begin
  6.       [...]
  7.       try
  8.         Connected := True;
  9.         MessageDlgPos('Access - select database', mtInformation, [mbOk], 0, pt.X, pt.Y-80);
  10.       except
  11.         on E:Exception do begin
  12.           MessageDlgPos('Access failed !', mtError, [mbOk], 0, pt.X, pt.Y-80);
  13.         end;
  14.       end;
  15.       connected := False;
  16.     end;
  17. end;  
  18.  

« Last Edit: December 23, 2024, 10:12:53 pm by xint »

cdbc

  • Hero Member
  • *****
  • Posts: 1808
    • http://www.cdbc.dk
Re: mrOK von MessageDlg in Form 2 schließt ebenfalls Form2.ShowModal
« Reply #1 on: December 23, 2024, 06:02:25 pm »
Hi
Welche 'Form' ist deine /haupt/-form ?!?
Nicht dass du die haupt form schließt, denn das schließt auch 'form2'...
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

xint

  • New Member
  • *
  • Posts: 18
Re: mrOK von MessageDlg in Form 2 schließt ebenfalls Form2.ShowModal
« Reply #2 on: December 23, 2024, 06:29:12 pm »
The main form is 'Form1'. Form2.ShowModal is called from there.
I can't figure out why Form2 is also closed after the return, but Form2.OnClose gets definitely triggered.
« Last Edit: December 23, 2024, 07:55:53 pm by xint »

cdbc

  • Hero Member
  • *****
  • Posts: 1808
    • http://www.cdbc.dk
Re: mrOK von MessageDlg in Form 2 schließt ebenfalls Form2.ShowModal
« Reply #3 on: December 23, 2024, 07:05:30 pm »
Hi
Ich schreibe weiter in englisch, forum gesetze...
Right, then we'll have to see some code from you, 'cause it sounds a wee bit suspicious... Preferably a compilable project...
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

dseligo

  • Hero Member
  • *****
  • Posts: 1458
Re: mrOK von MessageDlg in Form 2 schließt ebenfalls Form2.ShowModal
« Reply #4 on: December 23, 2024, 07:37:00 pm »
Does Form2 closes when you press BtnTestConnection button?
If so, then check that ModalResult property of BtnTestConnection is set to mrNone.

xint

  • New Member
  • *
  • Posts: 18
Re: mrOK von MessageDlg in Form 2 schließt ebenfalls Form2.ShowModal
« Reply #5 on: December 23, 2024, 08:01:26 pm »
First of all, I appreciate your help and I apologize for the German post. I didn't realize that the other topics were all in English ...
I have just rewritten my topics so that the language is correct now.

@dseligo
It's not the BtnTestConnection that causes the close of Form2. It's is closed, once MessageDlgPos() returns.

@cdbc
Can I simply pack the folder with the project and upload it here ?

Thanks !

dseligo

  • Hero Member
  • *****
  • Posts: 1458
Re: mrOK von MessageDlg in Form 2 schließt ebenfalls Form2.ShowModal
« Reply #6 on: December 23, 2024, 08:24:05 pm »
@dseligo
It's not the BtnTestConnection that causes the close of Form2. It's is closed, once MessageDlgPos() returns.

OK, but if ModalResult of the button is set it will behave like this.

Quote
@cdbc
Can I simply pack the folder with the project and upload it here ?

You can use menu Project, Publish Project (see attachment).

xint

  • New Member
  • *
  • Posts: 18
Re: mrOK of MessageDlg in Form 2 also closes Form2.ShowModal
« Reply #7 on: December 23, 2024, 08:39:49 pm »
Please find the zip file attached. Please do not kill me because of missing Try-Finally's - I just picked the project up again yesterday (-;

Glad that  clicked on 'Attachments'. I just found the 'notify me' option I was looking for.
« Last Edit: December 23, 2024, 10:15:48 pm by xint »

xint

  • New Member
  • *
  • Posts: 18
Re: mrOK von MessageDlg in Form 2 schließt ebenfalls Form2.ShowModal
« Reply #8 on: December 23, 2024, 08:49:36 pm »
It's not the BtnTestConnection that causes the close of Form2. It's is closed, once MessageDlgPos() returns.

OK, but if ModalResult of the button is set it will behave like this.

As I understand it, a ModalResult-button should close its dialog and then stop triggering further actions. It should not also close the form underneath it and from which the dialog was started.

But I'm definitely not the pro. That's why I was asking for assistance ...

cdbc

  • Hero Member
  • *****
  • Posts: 1808
    • http://www.cdbc.dk
Re: mrOK of MessageDlg in Form 2 also closes Form2.ShowModal
« Reply #9 on: December 23, 2024, 09:41:58 pm »
Hi
Ok, first things first, this:
Code: Pascal  [Select][+][-]
  1. MessageDlgPos
returns a "TModalResult" <-> integer, which you don't catch and who knows, if the form picks it up as its own result?!?
So, first try the code-snippet below, just to see, if that's indeed it...  :D
Code: Pascal  [Select][+][-]
  1. var
  2.   ModalRes: integer; // ~ TModalResult ~ catcher :o)
  3. begin
  4.     With ZConnection2 do begin
  5.       [...]
  6.       try
  7.         Connected := True;
  8. ///// try the below to see if that'll work /////
  9.         ModalRes:= MessageDlgPos('Access - select database', mtInformation, [mbOk], 0, pt.X, pt.Y-80);
  10.       except
  11.         on E:Exception do begin
  12. ///// try the below to see if that'll work /////
  13.           ModalRes:= MessageDlgPos('Access failed !', mtError, [mbOk], 0, pt.X, pt.Y-80);
  14.         end;
  15.       end;
  16.       connected := False;
  17. ///// as far as i can remember, modalresults are always positive /////
  18.       ModalRes:= -1; // should not interfere with anything
  19.     end;
  20. end;
  21.  
That's also why I didn't make 'ModalRes' of type 'TModalResult', but an integer...
"Ich drücke die Daumen"  ;)
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

tetrastes

  • Hero Member
  • *****
  • Posts: 630
Re: mrOK of MessageDlg in Form 2 also closes Form2.ShowModal
« Reply #10 on: December 23, 2024, 09:55:38 pm »
Code: Pascal  [Select][+][-]
  1.       object BtnTestConnection: TBitBtn
  2.         Left = 135
  3.         Height = 26
  4.         Top = 184
  5.         Width = 230
  6.         Anchors = []
  7.         Caption = 'Test Connection'
  8.         ModalResult = 1           // This is mrOK, not mrNone!
  9.         NumGlyphs = 2
  10.         OnClick = BtnTestConnectionClick
  11.         TabOrder = 3
  12.       end
  13.  

This is what @dseligo have written to you. Clicking this button closes Form2 even if there is no OnClick handler at all.

cdbc

  • Hero Member
  • *****
  • Posts: 1808
    • http://www.cdbc.dk
Re: mrOK of MessageDlg in Form 2 also closes Form2.ShowModal
« Reply #11 on: December 23, 2024, 09:58:47 pm »
Hi
Well spotted @dseligo & @tetrastes =^
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

xint

  • New Member
  • *
  • Posts: 18
Re: mrOK of MessageDlg in Form 2 also closes Form2.ShowModal
« Reply #12 on: December 23, 2024, 10:12:34 pm »
Code: Pascal  [Select][+][-]
  1.       object BtnTestConnection: TBitBtn
  2.         [...]
  3.         ModalResult = 1           // This is mrOK, not mrNone![/b]
  4.         NumGlyphs = 2
  5.         OnClick = BtnTestConnectionClick
  6.         TabOrder = 3
  7.       end
  8.  

This is what @dseligo have written to you. Clicking this button closes Form2 even if there is no OnClick handler at all.

Crap - that was it !!!
Thank you so much. I really need to rethink the way I work and stop copy 'n pasting objects.
I'm glad that Pascal is so restrictive and doesn't allow assignments in a IF construct, like C does. Otherwise I would have already made a few serious mistakes with my copying and pasting of source code.

@tetrastes Can I buy you a coffee ?
@dseligo Sorry. I misunderstood the meaning of your comment.

tetrastes

  • Hero Member
  • *****
  • Posts: 630
Re: mrOK of MessageDlg in Form 2 also closes Form2.ShowModal
« Reply #13 on: December 23, 2024, 10:56:14 pm »
@tetrastes Can I buy you a coffee ?
Thank you, nothing to.  :D

 

TinyPortal © 2005-2018