Problem getting Modal Result

Hi all,
I’m trying to create a password dialog box but it doesn’t seem to be returning the modal result correctly

On return Im testing for mrOK but I never seem to get mrOK returned

Can some one guide me on this

So the form is created such at runtime

procedure TConfig_Form.Test_Connect_BtnClick(Sender: TObject);
Var
  The_Form: TPasswordDlg;
begin
  try
    The_Form := TPasswordDlg.Create(Self);
    The_Form.Test := '12345';
    The_Form.ShowModal;
    if The_Form.ModalResult = mrOk then
      Test_Connect_Btn.Caption := 'yes'
    else
      Test_Connect_Btn.Caption := 'no';    *<<<<<<< but I only ever seem to get this
  finally
    if Assigned(The_Form) then
      FreeandNil(The_Form);
  end;
end;

and in the password dialog box I have

procedure TPasswordDlg.OKBtnClick(Sender: TObject);
begin
  if PASSWORD.Text = Test then
    ModalResult := mrOk
  else
    ModalResult := mrNo;
  Close;
end;

Thanks in advance
Grant

I assume you have checked and made sure that the OKBtn has its ModalResult property set to mrNone?

You can also use this code:

if The_Form.ShowModal = mrOK then

1 Like

unrelated to the question, I fixed your code formatting.

FYI, to have the forum syntax highlight code

````delphi
code goes here
````

If you edit your post you will see how it works.

From memory you don’t need to call close after you set the modal result of the form. Does it make a difference if you don’t call close?

It might make more sense or make for cleaner code to use the form’s OnCloseQuery method and just leave the button’s ModalResult as mrOk

1 Like

Seems to me a strange way of checking for modalresult. Why not just
“if The_Form.ShowModal = mrOK then”?

If you might have more than one important return type from the modal form …

1 Like