Jump to content
bernhard_LA

best practise sharing : 2 buttons with mrOK on a form

Recommended Posts

I want to evaluate  the pressed button on a form with this code below,  my form (aForm)  should have 2 buttons which the property mrOK.

If  aForm.modalresult = mrOK  then
      begin
          ///   now evaluate which button has been pressed ???
          ....
          ....
     end;

what  is the best solution for this issue?

 

Edited by bernhard_LA
typo

Share this post


Link to post

ModalResult is an integer value, so you can assign whatever you want, the form will close and ModalResult will hold this value.

Share this post


Link to post

There is no way to differentiate which control set the ModalResult if they both set it to the same value.  ShowModal() will close the Form when any non-zero value is assigned to ModalResult, you are not limited to the handful of constants that the RTL defines, use whatever values you want.

  • Like 1

Share this post


Link to post
23 hours ago, bernhard_LA said:

I want to evaluate  the pressed button on a form with this code below,  my form (aForm)  should have 2 buttons which the property mrOK.


If  aForm.modalresult = mrOK  then
      begin
          ///   now evaluate which button has been pressed ???
          ....
          ....
     end;

what  is the best solution for this issue?

 

 

I don't understand correctly your question... but usually on a dialog form you have 2 buttons with different results. mrOk / mrCancel or mrYes / mrNo.

if AForm.ShowModal = mrOk then
begin
	// Ok button pressed
	// do some stuff...
end
else 
begin
	// Cancel button pressed
	// do other stuff...
end;

Anyway, if you have to (for any strange reason) set 2 buttons with the same result values... you must store in a variable the user choise, button A or button B.

TMyDialogForm = class(TForm)
private
	FSelectedButton : Integer;
public
	property SelectedButton : Integer read FSelectedButton;
end;

//...

procedure TMyDialogForm.BtnAClick(Sender: TObject);
begin
	// Store the button pressed inside the FSelectedButton variable
	FSelectedButton := 1;
	ModalResult := mrOk;
end;

procedure TMyDialogForm.BtnBClick(Sender: TObject);
begin
	// Store the button pressed inside the FSelectedButton variable
	FSelectedButton := 2;
	ModalResult := mrOk;
end;

I hope it helps you.

 

Share this post


Link to post

One option would be to return two different "OK" in ModalResult from different buttons, like mrOK and mrYES.
If that is what you mean.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×