* * *

Author Topic: Modal  (Read 1076 times)

JLWest

  • Full Member
  • ***
  • Posts: 192
Modal
« on: February 08, 2018, 07:06:05 pm »
Have two Form project.

Unit1 and unit2 with Form1 and Form2.

Want to have Form1 call Form2.

Form2 has two buttons one button with modal set to mrOk
and the other modalresults set to mrNone.

This all works.

However If I code this in form1 it compiles. But when I run it and try to call form2 it throws an error at line 2029 in some unit.

What I want to do is run two forms with Form2 modal so when it return to Form1
I  can reload a listbox and make visual changes to Form1.

I don't want to use a button panel.

     
procedure TForm1.Button1Click(Sender: TObject);
begin
   Form2.Show;
    if form2.ShowModal = mrOK then
  begin
    ShowMessage('User pressed OK, need to check data.');
  end;
end;

Any Solution or help
Thanks.
 
JLWEST 
 FPC 3.0.4, Lazarus IDE v1.8.2 Windows 10 Pro
 Intel i7 770K CPU 4.2GHz 16 Gig Ram
GeForce GTX 1080 Graphics - 8 Gig 1.5 Terabyte SSD

Bart

  • Hero Member
  • *****
  • Posts: 3114
    • Bart en Mariska's Webstek
Re: Modal
« Reply #1 on: February 08, 2018, 07:18:26 pm »
This does not work for you?

Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.    //don't call this Form2.Show and then do:
  4.     if form2.ShowModal = mrOK then
  5.   begin
  6.     ShowMessage('User pressed OK, need to check data.');
  7.   end;
  8. end;
  9.  

Bart

Handoko

  • Hero Member
  • *****
  • Posts: 2604
  • My goal: build my own game engine using Lazarus
Re: Modal
« Reply #2 on: February 08, 2018, 07:28:22 pm »
Bart was faster. But I think my explanation may still provide some useful info.

I cannot fully understand what you meant or what you wanted do, but I think you didn't know what the 'modal' means.

The error can be solved by simply removing/commenting line #35. The error happens because you're not allow to call a form to show modal before you close the (non-modal) form. It is a bit hard to explain using words, something like this:

- Call Form.Show and then Form.Show ---> no problem
- Call Form.ShowModal and then Form.ShowModal ---> no problem
- Call Form.ShowModal and then Form.Show ---> no problem
- Call Form.Show and then Form.ShowModal ---> it is not allowed

You may ask why, it is understandable if you really understand what the 'modal' means.

Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.     Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.     Unit2;
  10.  
  11. type
  12.  
  13.     { TForm1 }
  14.  
  15.     TForm1 = class(TForm)
  16.         Button1: TButton;
  17.         procedure Button1Click(Sender: TObject);
  18.     private
  19.  
  20.     public
  21.  
  22.     end;
  23.  
  24. var
  25.     Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. { TForm1 }
  32.  
  33. procedure TForm1.Button1Click(Sender: TObject);
  34. begin
  35. //   Form2.Show;
  36.     if form2.ShowModal = mrOK then
  37.   begin
  38.     ShowMessage('User pressed OK, need to check data.');
  39.    end;
  40. end;
  41.  
  42. end.
« Last Edit: February 08, 2018, 07:35:08 pm by Handoko »

JLWest

  • Full Member
  • ***
  • Posts: 192
Re: Modal
« Reply #3 on: February 08, 2018, 07:50:09 pm »
So I commented out the line and ran the program but the message didn't appear when I hit the button sset to modal mrOk. Is it suppose to?

JLWEST 
 FPC 3.0.4, Lazarus IDE v1.8.2 Windows 10 Pro
 Intel i7 770K CPU 4.2GHz 16 Gig Ram
GeForce GTX 1080 Graphics - 8 Gig 1.5 Terabyte SSD

Handoko

  • Hero Member
  • *****
  • Posts: 2604
  • My goal: build my own game engine using Lazarus
Re: Modal
« Reply #4 on: February 08, 2018, 07:54:05 pm »
I never tried, but no ... you should not do thing like that.

If you need a non-modal form, you simply call TForm.Show. If you need a modal form, you call TForm.ShowModal.

Don't combine them.


Wait, I'm testing your issue.
« Last Edit: February 08, 2018, 08:08:21 pm by Handoko »

Bart

  • Hero Member
  • *****
  • Posts: 3114
    • Bart en Mariska's Webstek
Re: Modal
« Reply #5 on: February 08, 2018, 08:13:18 pm »
Well, then ShowModal did retun something else than mrOK.
Just inspect the value.

Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   Res: TModalResult;
  4. begin
  5. //   Form2.Show;
  6.   Res := form2.ShowModal;
  7.     if Res  = mrOK then
  8.   begin
  9.     ShowMessage('User pressed OK, need to check data.');
  10.    end
  11.   else
  12.   begin
  13.     ShowMessage(Format('Res = %d, but I expected %d',[Res,mrOk]));
  14.   end;
  15. end;
  16.  

Bart

Handoko

  • Hero Member
  • *****
  • Posts: 2604
  • My goal: build my own game engine using Lazarus
Re: Modal
« Reply #6 on: February 08, 2018, 08:15:34 pm »
I understood the case now and I have fixed the code. The bug is he called TForm.Close on the button that return the modal result.

On Unit1, call ShowModal only once and don't combine it with Form2.Show:

Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   if Form2.ShowModal = mrOK then
  4.   begin
  5.     ShowMessage('User pressed OK, need to check data.');
  6.   end;
  7. end;

On Unit2, don't call Close on the button that return modal result:

Code: Pascal  [Select]
  1. procedure TForm2.Button2Click(Sender: TObject);
  2. begin
  3. //  Close;
  4. end;

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus