* * *

Author Topic: Can I ask a Dumb Questions?  (Read 851 times)

JLWest

  • Full Member
  • ***
  • Posts: 100
Can I ask a Dumb Questions?
« on: January 31, 2018, 06:53:44 am »
The subject line is just like when you call for an appointment. They always say "Can you hold?" click.

I always wonder "Did I have a choice there?"

Anyway, I have a program with two screens.

Form1  with a button that calls Form2.
When I close or exit Form2 and return to Form1 where dose it return to. Is to the
buttonClicked CallForm2?

I ask because I need to check if the user changed something on Form2.


JLWEST

GetMem

  • Hero Member
  • *****
  • Posts: 2742
Re: Can I ask a Dumb Questions?
« Reply #1 on: January 31, 2018, 07:04:59 am »
Use Form2.ShowModal instead of Form2.Show. Please test attached project, first close form2 with button "Cancel" then with button "OK". You will see the difference.

JLWest

  • Full Member
  • ***
  • Posts: 100
Re: Can I ask a Dumb Questions?
« Reply #2 on: January 31, 2018, 07:45:48 am »
@GetMan

Thank you
JLWEST

JLWest

  • Full Member
  • ***
  • Posts: 100
Re: Can I ask a Dumb Questions?
« Reply #3 on: January 31, 2018, 08:12:49 am »
I see the difference but I don't see where to set Form2 to modal.
JLWEST

GetMem

  • Hero Member
  • *****
  • Posts: 2742
Re: Can I ask a Dumb Questions?
« Reply #4 on: January 31, 2018, 08:16:14 am »
Quote
I see the difference but I don't see where to set Form2 to modal.
By calling Form2.ShowModal instead of Form2.Show.

JLWest

  • Full Member
  • ***
  • Posts: 100
Re: Can I ask a Dumb Questions?
« Reply #5 on: January 31, 2018, 08:27:15 am »
Maybe I understand

So in the button on my Form1 I code at the //save  I can check what I have to and make calls or changes to Form1 info.

 "if form2.ShowModal = mrOK then
  begin
    ShowMessage('User pressed OK, need to check data.');
    //save
  end; "

I was looking for something in the properties of Form2 that you would set.

                                     
« Last Edit: January 31, 2018, 08:34:34 am by JLWest »
JLWEST

GetMem

  • Hero Member
  • *****
  • Posts: 2742
Re: Can I ask a Dumb Questions?
« Reply #6 on: January 31, 2018, 08:45:35 am »
Quote
So in the button on my Form1 I code at the //save  I can check what I have to and make calls or changes to Form1 info.
Yes. For example drop a TEdit both to form1 and form2.
Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   if Form2.ShowModal = mrOK then
  4.   begin
  5.     Edit1.Text := Form2.Edit1.Text;
  6.   end;
  7. end;
If a user types something to Form2's Edit1 and press the OK button, the modification will be visible on Form1 Edit1.

RayoGlauco

  • New member
  • *
  • Posts: 14
Re: Can I ask a Dumb Questions?
« Reply #7 on: January 31, 2018, 09:21:33 am »
You can put some variables in the "public" section of the Form2 declaration. Form1 can put data in these variables, then call Form2. Form2 can check/change these values too. And finally, when Form2 is closed, Form1 can check these values again.
« Last Edit: January 31, 2018, 09:24:14 am by RayoGlauco »

wp

  • Hero Member
  • *****
  • Posts: 4178
Re: Can I ask a Dumb Questions?
« Reply #8 on: January 31, 2018, 09:30:23 am »
"if form2.ShowModal = mrOK then
  begin
    ShowMessage('User pressed OK, need to check data.');
    //save
  end; "
If you want to check data which the user has input into Form2 I would not put this code into Form1. Encapsulation is one of the principles of object-oriented programming. Only Form2 knowns which controls it contains. Form1 should not rely on such details. The practical background is: Suppose you change something on Form2 then you always must check also Form1 whether input validation is not affected there too.

Usually I write a function ValidInput(out AMsg: String; out AControl: TWinControl): Boolean which belongs to the form class which is to be checked (here: TForm2). It returns false, if something is wrong and, in this case, returns the guilty control as well as an error message. It is called from the OnClick event of the OK button on TForm2; in case of an error the error message is displayed, and the guilty control is focused, and - what's most important - the form does not close and gives the user a chance to fix his input (or press "Cancel"):

Code: Pascal  [Select]
  1. procedure TForm2.OKButtonClick(Sender: TObject);
  2. var
  3.   C: TWincontrol;
  4.   msg: String;
  5. begin
  6.   if not ValidInput(msg, C) then begin
  7.     C.SetFocus;
  8.     MessageDlg(msg, mtError, [mbOK], 0);
  9.     ModalResult := mrNone;   // <--- the prevents the form from closing
  10.   end;
  11. end;
  12.  
  13. function TForm2.ValidInput(out AMsg: String; out AControl: TWinControl): boolean;
  14. var
  15.   x: Double;
  16. begin
  17.   Result := false;
  18.   // now you put code to check the data in the controls, e.g.
  19.   if Edit1.Text = '' then begin
  20.     AMsg := 'Input required.';
  21.     AControl := Edit1;
  22.     exit;
  23.   end;
  24.   if not TryStrToFloat(Edit1.Text, x) then begin
  25.     AMsg := 'No valid number.';
  26.     AControl := Edit1;
  27.     exit;
  28.   end;
  29.   Result := true;
  30. end;

The attached demo project applies this principle to an input form with two edits which require numerical input and the second value always must be greater than the first one. It goes a step further by defining properties to pass the input data to the calling form as double values instead of strings - this way the calling form never must know anything about the internal structure of TForm2.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

GetMem

  • Hero Member
  • *****
  • Posts: 2742
Re: Can I ask a Dumb Questions?
« Reply #9 on: January 31, 2018, 09:49:40 am »
@wp
Ok. If it's about data validation, then most definitely it should be done before Form2.Close, but then the following statement of OP is misleading:
Quote
Form1  with a button that calls Form2.
When I close or exit Form2 and return to Form1 where dose it return to. Is to the
buttonClicked CallForm2?
I ask because I need to check if the user changed something on Form2.
Just by pressing the OK button we can assume that the user made some significant changes, otherwise he would pressed the cancel button. I was under the impression that the OP wish to process a data entered on Form2, of course I might be wrong.

wp

  • Hero Member
  • *****
  • Posts: 4178
Re: Can I ask a Dumb Questions?
« Reply #10 on: January 31, 2018, 10:14:54 am »
GetMem, I had the same impression, but the OP also wrote (as I cited) "User pressed OK, need to check data." And the "need to check data" triggered me to describe what data validation should look like (in my opinion).
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

JLWest

  • Full Member
  • ***
  • Posts: 100
Re: Can I ask a Dumb Questions?
« Reply #11 on: January 31, 2018, 03:56:52 pm »
Wow This dumb question really got complicated fast. Can I put you guys on hold. Click!

First I still don't understand how the little example program works. (internally that is)

It can't just be the code below. Isn't something set on the cancel button. Form2 has two buttons. an OK and a Cancel. Hit the Ok on Form2 and I  get a modal close, Hit Cancel on Form2 and I get a regular close. How do the buttons know?

"if form2.ShowModal = mrOK then
  begin
    ShowMessage('User pressed OK, need to check data.');
    //save
  end; "
JLWEST

GetMem

  • Hero Member
  • *****
  • Posts: 2742
Re: Can I ask a Dumb Questions?
« Reply #12 on: January 31, 2018, 05:46:57 pm »
Quote
First I still don't understand how the little example program works. (internally that is)

It can't just be the code below. Isn't something set on the cancel button. Form2 has two buttons. an OK and a Cancel. Hit the Ok on Form2 and I  get a modal close, Hit Cancel on Form2 and I get a regular close. How do the buttons know?
Buttons have a property named ModalResult. When you click a button with ModalResult = mrOk for example, it will immediately close the modal form with mrOk as result. This is already implemented in LCL, so you don't have to worry about it. Now can you please explain what exactly are you trying to achieve?

JLWest

  • Full Member
  • ***
  • Posts: 100
Re: Can I ask a Dumb Questions?
« Reply #13 on: January 31, 2018, 08:37:36 pm »
 Now can you please explain what exactly are you trying to achieve?

Yes; I will.

I have a two Form program.
The program opens with Form1 (Main Screen) and can call or open Form2 (Setup Screen).

Settings can be made on Form2 that have to be reflected on Form1 and may require additional action/services be performed by Form1.
 
JLWEST

JLWest

  • Full Member
  • ***
  • Posts: 100
Re: Can I ask a Dumb Questions?
« Reply #14 on: January 31, 2018, 09:07:27 pm »
OK - I see Modalresult on Button1.

However; on Form2 the buttons are in a panel and I cannot see a property on the OK button of ModalResult which I would expect. Or one on the Cancel Button.

JLWEST

 

Recent

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