Message display in subform in delphi
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 erro开发者_C百科r 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;
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;
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;
精彩评论