Recent

Author Topic: [SOLVED]Sending parameters to a modal form and retrieving the modified parameter  (Read 9154 times)

JD

  • Hero Member
  • *****
  • Posts: 1913
Hi there everyone,

I'm having some trouble with sending a variable to a modal form, modifying the variable and then acting on the value of the modified variable.

I send a variable ACancelOperation (among others) to a modal form. Initially the value of ACancelOperation is 'No'. If the user clicks the "Close" button on the modal form; the FCancelOperation in the form is set to 'Yes'. The code is as shown below: 

Code: [Select]
procedure TfrmServer.acParametersExecute(Sender: TObject);
{ Displays the server parameters form which updates the configuration file }
var
  ParametersForm: TfrmParameters;
  ACancelOperation: string;
begin
  // Initialize the ACancelOperation variable
  ACancelOperation := 'No';
  // Create the parameters form
  Application.CreateForm(TfrmParameters, ParametersForm);
  // Send the port number and the number of maximum connections to the parameters form
  ParametersForm.GetParameters(FPort, FMaxConnections, FOneConnectionPerClient, ACancelOperation);
  try
    // Display the form modally
    ParametersForm.ShowModal;
  finally
    // Free the parameters form
    ParametersForm.Free;
  end;
  // Make the restart label visible if the user clicked on the Close button in the Parameters form
  if ACancelOperation = 'No' then
    lblRestart.Visible := True;
end;

The code in the Parameters form is shown below:
Code: [Select]
procedure TfrmParameters.GetParameters(APort, AMaxConnections, AOneConnectionPerClient: string; var ACancelOperation: string);
begin
  FPort := APort;
  FMaxConnections := AMaxConnections;
  FOneConnectionPerClient := AOneConnectionPerClient;
  FCancelOperation := ACancelOperation;
end;

procedure TfrmParameters.bbtnCloseClick(Sender: TObject);
begin
  //
  FCancelOperation := 'Yes';
end;

By the way, I tried

a) using "var" attempting to pass the ACancelOperation parameter by reference
b) using a boolean variable

Neither (a) nor (b) worked!  :(

What am I doing wrong?

JD
« Last Edit: October 24, 2012, 05:35:19 pm by JD »
Linux Mint - Lazarus 4.6/FPC 3.2.2,
Windows - Lazarus 4.6/FPC 3.2.2

mORMot 2, PostgreSQL & MariaDB.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Sending parameters to a modal form and retrieving the modified parameter
« Reply #1 on: October 24, 2012, 04:26:02 pm »
The parameters were propably passed ok, but you don't see to copy the variables in visual components at all, so user can see them.

Then i wouldn't use global variables, but get use the modalform for that:
Code: [Select]
  try
    // Display the form modally
    ParametersForm.ShowModal;
    // Make the restart label visible if the user clicked on the Close button in the Parameters form
    if ParametersForm.ACancelOperation = 'No' then // Make it a public property for form class
      lblRestart.Visible := True;
  finally
    // Free the parameters form
    ParametersForm.Free;
  end;
Or you could use ModalResult that is default way to deal with modal forms. Like:
Code: [Select]
if ParametersForm.ShowModal = mrOK then
« Last Edit: October 24, 2012, 04:29:39 pm by User137 »

JD

  • Hero Member
  • *****
  • Posts: 1913
Re: Sending parameters to a modal form and retrieving the modified parameter
« Reply #2 on: October 24, 2012, 04:40:12 pm »
The parameters were propably passed ok, but you don't see to copy the variables in visual components at all, so user can see them.

Then i wouldn't use global variables, but get use the modalform for that:
Code: [Select]
  try
    // Display the form modally
    ParametersForm.ShowModal;
    // Make the restart label visible if the user clicked on the Close button in the Parameters form
    if ParametersForm.ACancelOperation = 'No' then // Make it a public property for form class
      lblRestart.Visible := True;
  finally
    // Free the parameters form
    ParametersForm.Free;
  end;
Or you could use ModalResult that is default way to deal with modal forms. Like:
Code: [Select]
if ParametersForm.ShowModal = mrOK then

The parameters are passed correctly. I verified them in the modal form's OnShow method.

I've already tried the modification you suggested and it dosen't work. In addition, I'm not using global variables. I avoid global variables as much as I can. The variables I pass are private variables.
Linux Mint - Lazarus 4.6/FPC 3.2.2,
Windows - Lazarus 4.6/FPC 3.2.2

mORMot 2, PostgreSQL & MariaDB.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Sending parameters to a modal form and retrieving the modified parameter
« Reply #3 on: October 24, 2012, 05:03:53 pm »
More information needed, or source codes. Cannot tell what is not working, and how "it" is currently done.

JD

  • Hero Member
  • *****
  • Posts: 1913
Re: Sending parameters to a modal form and retrieving the modified parameter
« Reply #4 on: October 24, 2012, 05:08:36 pm »
More information needed, or source codes. Cannot tell what is not working, and how "it" is currently done.

What information do you need? The code that creates, displays and releases the modal form is shown above. The modification of the ACancelOperation variable's value is also provided.

Thanks
« Last Edit: October 24, 2012, 05:11:02 pm by JD »
Linux Mint - Lazarus 4.6/FPC 3.2.2,
Windows - Lazarus 4.6/FPC 3.2.2

mORMot 2, PostgreSQL & MariaDB.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Sending parameters to a modal form and retrieving the modified parameter
« Reply #5 on: October 24, 2012, 05:15:59 pm »
If ACancelOperation is set to 'Yes' in modalform, and you read ParametersForm.ACancelOperation, it should also be 'Yes'.

Also i read your code again, you don't need duplicates. So either FCancelOperation or ACancelOperation can go away  :)  But main program needs to have access to that result from modal form.

JD

  • Hero Member
  • *****
  • Posts: 1913
Re: Sending parameters to a modal form and retrieving the modified parameter
« Reply #6 on: October 24, 2012, 05:20:39 pm »
If ACancelOperation is set to 'Yes' in modalform, and you read ParametersForm.ACancelOperation, it should also be 'Yes'.

Also i read your code again, you don't need duplicates. So either FCancelOperation or ACancelOperation can go away  :)  But main program needs to have access to that result from modal form.

Sorry about the mixup. You were right. I didn't see it when you suggested it earlier. In the end the answer is so simple that it is obvious - Testing for the modal result was all that was needed.

Code: [Select]
if ParametersForm.ShowModal = mrOK then
It completely skipped my mind. The ACancelOperation variable is not necessary at all.
I've been behind this computer screen for too long today.  :D

Thanks a lot,

JD
« Last Edit: October 24, 2012, 05:22:33 pm by JD »
Linux Mint - Lazarus 4.6/FPC 3.2.2,
Windows - Lazarus 4.6/FPC 3.2.2

mORMot 2, PostgreSQL & MariaDB.

 

TinyPortal © 2005-2018