55 Votes

Delphi/Lazarus: ShowModal Result

Tip by Stefan Trost | Last update on 2024-02-28 | Created on 2013-05-19

ShowModal shows a window and waits until it is closed. Until closing, you cannot interact with any other window of the application. What many don't know: You can define a result value on closing of the ShowModal Form, which you can then evaluate comfortably in your program.

For example to check which button the user has clicked.

Example

Let's have a look at an simple example demonstrating this. Our first form is Form1 and we want to display a second modal window (Form2) using ShowModal.

On Form2, we have placed two buttons to which we have assigned the following code:

procedure TForm2.Button1Click(Sender: TObject);
begin
  ModalResult := 1;
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
  ModalResult := 2;
end;

In Form1, we are showing this form with ShowModal:

procedure TForm1.Button1Click(Sender: TObject);
var
  k: integer;
begin
  // Example 1
  k := Form2.ShowModal;
  if k = 1 then ShowMessage('Button 1 was clicked!');
  if k = 2 then ShowMessage('Button 2 was clicked!');

  // Example 2
  if Form2.ShowModal = 1 then begin
    ShowMessage('Button 1 was clicked!');
  end;
end;

The value we have assigned to ModalResult in the ButtonClick procedures is the result of the ShowModal call. At the same time we are setting ModalResult to another value than zero, the form is closed automatically.

In the example, in the code of Form2, we are setting ModalResult to 1 with Button1 and to 2 with Button2. So, we can check in the code of Form1 which button was clicked on Form2 and we can show corresponding messages for our the example.

ModalResult Buttons

Next to this method, it is also possible to directly apply one of the standard ModalResults to our buttons. For this, buttons are having the property "ModalResult" which we can set (for example via the Object Inspector) to mrOK, mrCancel, mrYes or mrNo as an example.

If we have prepared a button in this way, we can retrieve the result in the following way:

if Form2.ShowModal = mrOk then begin
   ShowMessage('The Button with mrOk was clicked!');
end;

So if a user has clicked on the button with the property ModalResult = mrOK, the respective form closes automatically and automatically returns mrOk as the result to us. And this without us having to change any other properties of the button or assigning any event or code to the button. In addition, we can see from the return value of ShowModal which button was clicked by the user.

ReplyPositiveNegative

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

Delphi: System-Wide HotKey

Tutorial | 1 Comment

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.