There are two forms.
In Form1, Form2 is created, and then Form2, closed with the TBitBtn button

Unit1 Module:

procedure TForm1.Button1Click(Sender: TObject); begin Form2 := TForm2.Create(self); Form2.ShowModal; if Form2.ModalResult = mrOK then begin showmessage('on Unit2 click "OK"'); end; Form2.Free; end; 

Unit2 Module:

 procedure TForm2.BitBtn1Click(Sender: TObject); begin if StrToInt(Edit1.Text) > 10 then begin ShowMessage('"true", exit from procedure"'); exit; end; end; 

When you click a button, you must not close Form2 if the condition in unit2 is true.
Question:
Why with ModalResult: = mrOK (component “TBitBtn”) - Form2 - closes without responding to “exit”, and the execution of the code “ if Form2.ModalResult = mrOK then ” in Unit1 continues.
And how do I with “ModalResult: = mrOK”, if the condition is met, it’s certainly not to close?
You can certainly do the flags, but it's interesting all the same in this way

    2 answers 2

    The process goes something like this:

    1. The form receives a click event (and determines where it falls on the button).
    2. Calls the procedure - button click handler (your code).
    3. If the button has a ModalResult , then it assigns it to itself.
    4. Checks the state of its ModalResult and if it is not mrNone , then it closes with it.

    That is, leaving your Exit; handler for Exit; You do not change the external flow of the code.

    You can affect by calling an exception (for example, Abort; ), but this is fraught with other troubles.


    Make it easier, remove ModalResult = mrOK from the button and put the result in the procedure as you like:

     procedure TForm2.BitBtn1Click(Sender: TObject); begin if StrToInt(Edit1.Text) > 10 then ShowMessage('"true", exit from procedure"') else ModalResult := mrOk; end; 
    • Yes, it really does work. plain and simple. Thank you - Konstantin78
    • Well, I just still wanted to know what the " exit " ignoring chip is, with " ModalResult: = mrOK "? - Konstantin78
    • @ Konstantin78 updated the answer - Kromster
    • Thanks again for the clarification ... (PS and "Abort", I have already tried, the effect is the same - but oh well, that's another story) - Konstantin78
    • @ Konstantin78 Here you have to look at the VCL code, it is possible that the ModalResult assigned before calling the handler. Or there exceptions are somehow caught. Or more options. - Kromster

    In this case, it is necessary to check the condition not in the event of pressing the button, but in the event of closing the window - i.e. in the OnClose event (respectively, return Action = caNone).

     procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction); begin if StrToInt(Edit1.Text) > 10 then Action := caNone; end; 

    There is also the OnCloseQuery event, but I do not advise using it, because there it is necessary to handle the system shutdown situation (see this question ).

    • What are the rules? No need to add additional entities here. - Kromster
    • How I felt that they would come to the word. I paraphrase: "When you click the button, you must not close Form2, if the condition in unit2 is true." - to check the condition when the form is closed there is an OnClose event. - Alekcvp
    • Nobody finds fault with words. If the author uses ModalResult then it is enough to operate only with them. There is no need to interfere here with OnClose / OnCloseQuery and complicate the logic additionally. - Kromster