Topic: MessageDlg problem

Good morning,

I have a MessageDlg as per the code below - however, when the user clicks cancel (mrNo) the form still closes. Please what am I doing wrong??

procedure frmColours_btnClose_OnClick (Sender: TObject; var Cancel: boolean);
begin
    if frmColours.edColourID.GetTextLen > 0 then
        begin
            if MessageDlg('Are you sure you wish to discard your changes? '+frmColours.edColourID.text, mtConfirmation,mbNo+mbYes,0) = mrNo
            //if mrNo
            then // Cancel pressed
                begin
                    ShowMessage('Dont Close (testing procedure)');
                    frmColours.edColourID.SetFocus;
                end
            else
               ShowMessage('Closing (testing procedure)'); // Yes pressed
               frmColours.Close;
        end;
end;

2 (edited by derek 2020-12-25 13:45:58)

Re: MessageDlg problem

Hi Jason,
You need another 'begin.....end' after your 'else' because you have more than one instruction in that condition.
If you have just 'else' (without a 'begin.....end'), it will conditionally execute the first instruction but then unconditionally execute the second (which is why it is always closing frmcolours)
Please see the attached.
Derek.

Post's attachments

Attachment icon jason.zip 335.5 kb, 263 downloads since 2020-12-24 

Re: MessageDlg problem

derek wrote:

Hi Jason,
You need another 'begin.....end' after your 'else' because you have more than one instruction in that condition.
If you have just 'else' (without a 'begin.....end'), it will conditionally execute the first instruction but then unconditionally execute the second (which is why it is always closing frmcolours)
Please see the attached.
Derek.

Awesome - thank you for your assistance smile