Recent

Author Topic: Always get f.ShowModal = mrCancel  (Read 5782 times)

dkjMusic

  • Full Member
  • ***
  • Posts: 146
Always get f.ShowModal = mrCancel
« on: December 26, 2010, 09:46:55 pm »
Here is my code to display a confirmation form which has two buttons, one named AcceptButton with ModalResult set to mrOK and the other named CancelButton with ModalResult set to mrCancel. Both buttons have Close in their OnClick event. No matter which button I click when the Confirm_Form is displayed, the following function returns Result=False. I cannot figure out for the life of me what I'm missing.

function TForm1.ConfirmNewProject(DName: String): Boolean;
var
  f: TConfirm_Form;
begin
  f := TConfirm_Form.Create(Self);
  f.Label2.Caption := DName;
  try
    if f.ShowModal = mrOk then Result := True
       else Result := False;
  finally
    f.Release;
  end;
end;
Windows 7 Ultimate/32 bit;
Lazarus 0.9.31/Rev 33300;
FPC 2.7.1/Rev 19464;
GDB 7.0.50;
(via CodeTyphon 2.30)

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Always get f.ShowModal = mrCancel
« Reply #1 on: December 26, 2010, 10:10:04 pm »
Add a boolean variable on your first unit called OKButton and set its value on respective close event handlers. Then declare each unit on the uses section of the other (on the implementation section). Remove the form creation of the automatic creation on the project file. Then try this:

Code: [Select]
begin
  // you don't need to create a form that already exists
  Application.CreateForm(TConfirm_Form, Confirm_Form);
  Confirm_Form.Label2.Caption := DName;
  try
    Confirm_Form.ShowModal;
    if OKButton then
      Result := True
    else
      Result := False;
  finally
    Confirm_Form.Free;
  end;
end;   

« Last Edit: December 26, 2010, 10:20:32 pm by typo »

dkjMusic

  • Full Member
  • ***
  • Posts: 146
Re: Always get f.ShowModal = mrCancel
« Reply #2 on: December 26, 2010, 10:14:11 pm »
I guess I was using the whole modal form thing wrong. I took out the button event handlers and everything worked fine. The modal form closes automatically when one  of the buttons is pressed.
Windows 7 Ultimate/32 bit;
Lazarus 0.9.31/Rev 33300;
FPC 2.7.1/Rev 19464;
GDB 7.0.50;
(via CodeTyphon 2.30)

 

TinyPortal © 2005-2018