views:

229

answers:

2

messagebox (handle, 'do you really want to exit?', 'are you sure?', 1);

in this button there are two things, what the user can do. ok and cancel. what code do i have to write, that the button closes the program at "ok" and ends the dialog, when pressing cancel?

+3  A: 

First, make sure that the buttons in the message box match the text. So if the text is "Do you really want to exit?" then the buttons should be "Yes" and "No".

Second, use the appropriate constants, so that your code is easier to read later on. That would be:

var
  Res: integer;

Res := Application.MessageBox('Do you really want to exit?", "Are you sure?",
  MB_ICONQUESTION or MB_YESNO);

The result will then be either IDYES or IDNO. So assuming that the call is inside a method of your main form, you would use it like:

if Res = IDYES then
  Close;

If you call this from another place, you could also call

if Res = IDYES then
  Application.Terminate;

Edit: Please do also check out the Vista User Inteface Guidelines on dialog boxes which state:

Unnecessary confirmations are annoying

mghie
+2  A: 

Delphi provides better solutions for showing a messagebox. I should use the MessageDlg function. The return value of the MessageDlg (and MessageBox) function indicates the users choice. So you when you place a yes button on the MessageDlg, the return value will be mrYes when the user presses the Yes button. So your code would be like this:

var
  ShouldClose: Boolean;
begin
  if MessageDlg('Do you really want to quit?', mtConfirmation, 
      [mbYes, mbNo], 0) = mrYes then
    ShouldClose := True
  else
    ShouldClose := False;
end;

You also want to close your application if the users chooses Yes. When you have a normal Delphi VCL application you can implement the CloseQuery event of you mainform, the CloseQuery event is executed when you try to close your mainform (like clicking the closebutton) and has a variable CanClose. Setting CanClose to True means the MainForm is OK to close, setting it to false will prevent your mainform from closing:

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := MessageDlg('Do you really want to quit?', mtConfirmation, 
    [mbYes, mbNo], 0) = mrYes;
end;
The_Fox
MessageDlg is nice, but is not localized. If he is writing non English programs and do not use any method to localize, I suggest use Application.MessageBox. Application.MessageBox, will load the windows dialogs in windows localized version.
Cesar Romero
when i copy and paste your first code, the message box appears, but no matter what i click on, nothin happens, the box just closes.what do i have to write for shouldclose , that it closes the program?
Alex
When you have a new Delphi VCL application: Add a button, in it's handler add: Close;Go to your form, go to events, add an FormClose event. Make the eventhandler look like my second code sample.
The_Fox
When you don't want to work with an OnCloseQuery event, add this to my first codesample: if ShouldClose then Close;
The_Fox