2

I am creating one subForm from Main form on button click event of Main Form.Now On subForm I have added one button named 'OK'.ModalResult property of Button set to mrOK. On Ok button click i want to peform some validations. If there is some error i want to show it on subform and should focus on the error filed of SubForm. But I am able to show error message and after error msg displayed Subform closes and main form will be displayed. Below is part of code. Plz help me.

result:= frmAddField.showModal= mrOK; // subForm

procedure TfrmAddField.btnOKClick(Sender:TObject);
begin
  if edit1.text = '' then
  begin
    MessageDlg('Error',mtWarning,[mbOK],0);
    edit1.setfocus;
    break;
  end;
 // to be continued
end;
1
  • [Error] SubForm.pas(53): BREAK or CONTINUE outside of loop Commented Aug 17, 2011 at 14:48

2 Answers 2

5

Set the ModalResult property on the Button back to mrNone. Alter your event handler:

procedure TfrmAddField.btnOKClick(Sender:TObject); 
begin 
  if edit1.text = '' then 
  begin 
    MessageDlg('Error',mtWarning,[mbOK],0); 
    edit1.setfocus; 
  end else
    ModalResult := mrOK;
end;
Sign up to request clarification or add additional context in comments.

Comments

2

Break is not what you want here. Use Exit if you want to leave the current function or procedure. Also make sure that you only set the modal result if you want the form to close, in your example:

procedure TfrmAddField.btnOKClick(Sender:TObject);
begin
  if edit1.text = '' then
  begin
    MessageDlg('Error',mtWarning,[mbOK],0);
    edit1.setfocus;
    Exit;
  end;     
 // to be continued
 ModalResult := mrOK;          // validation worked, close the form!
end;  

2 Comments

I know it has the same effect, but I think that Abort conveys the meaning better that exit.
actually having break outside of a loop should not compile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.