Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Easy! MessageDlg with default button mbCancel does not work..

Posted on 2003-11-06
13
Medium Priority
?
1,284 Views
Last Modified: 2010-04-05
Hi Experts!

I copy example of MessageDlg from Delphi Help to blank project and it does not work!

procedure TForm1.Button1Click(Self);
begin
  if MessageDlg('Welcome to my Delphi application.  Exit now?',
    mtConfirmation, [mbYes, mbNo], 0, mbYes) = mrYes then
  begin
   //...
  end;
end;

Compiler message: Too many actual parametes.. it doesn't like second  ", mbYes)"
Q. Howto display MessageGld with default button set to mbNo (mbCancel...)?

Sincerely,
Boris
env. D7 / WinXP sp2
0
Comment
Question by:odissey1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
  • +2
13 Comments
 
LVL 27

Assisted Solution

by:kretzschmar
kretzschmar earned 200 total points
ID: 9699354
seems the delphi-help is wrong in this case,

but you can use the windows.messagebox instead like

procedure TForm1.Button1Click(sender : TObject);
begin
  if windows.MessageBox(self.handle
                       ,'Welcome to my Delphi application.  Exit now?'
                       ,'Message'
                       ,MB_ICONINFORMATION or MB_YESNO or MB_DEFBUTTON2) = mrYes then
  begin
   //...
  end;
end;

meikl ;-)
0
 

Expert Comment

by:huiyue
ID: 9699607
Look this :
procedure TForm1.Button1Click(Sender: TObject);
begin
  if messagedlg('fsdf',mtwarning,[mbyes,mbno],0)=mryes then
    showmessage('fda');
end;

procedure TForm1.Button1Click(Self);
begin
  if MessageDlg('Welcome to my Delphi application.  Exit now?',
    mtConfirmation, [mbYes, mbNo], 0, mbYes) = mrYes then
                                                       //the up word "mbyes" s otiose
  begin
   //...
  end;
end;
0
 
LVL 5

Expert Comment

by:DeerBear
ID: 9700476
hi,

Did you use the Dialogs unit for your form?

HTH,

Andrew
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 27

Expert Comment

by:kretzschmar
ID: 9700510
i tried it, also explicit (dialogs.mes...) with d7
-> described error comes up
0
 
LVL 5

Expert Comment

by:DeerBear
ID: 9700525
Hi,

Yeah, the Delphi help seems wrong in this case.
Just remove the second mbYes and it should work.

HTH,

Andrew
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9700537
>Just remove the second mbYes and it should work.
then you have no control about the default-button
0
 
LVL 5

Expert Comment

by:DeerBear
ID: 9700558
Uh?

I always stopped to the HelpCtx param( but I have only D6 ).

Cheers,

Andrew
0
 
LVL 23

Accepted Solution

by:
Ferruccio Accalai earned 200 total points
ID: 9700568
well guys...

The error is in the Help Of Course....

That call for MessageDlg with DefaultButton is a QDialogs procedure , not Dialogs (maybe it'll come with D8...)
BTW the hasker want to know how to set mbNO or mbCancel as default button...

well, there are 2 ways...
the first is to rewrite the dialogs unit for the messagedlg and set the defaultbutton in CreateMessageDialog function...

the second, maybe a little dirty but more easy, is to change the taborder in the dialog...look this example...

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
   
  end;

var
  Form1: TForm1;
  aMsgDlg: TForm;
  i,x: Integer;
  dlgButton: TButton;
  ButtonNames: array[TMsgDlgBtn] of string = ('Yes', 'No', 'OK', 'Cancel', 'Abort', 'Retry', 'Ignore', 'All', 'NoToAll',
    'YesToAll', 'Help');
 
implementation

{$R *.dfm}
function MyMessageDialog(const Msg: string; DlgType: TMsgDlgType;  Buttons: TMsgDlgButtons; Default: TmsgDlgBtn): Integer;
begin
  aMsgDlg := CreateMessageDialog(Msg, DlgType, Buttons);
  try
  for i := aMsgDlg.ComponentCount - 1 downto 0 do
  begin
    if (aMsgDlg.Components[i] is TButton) then
    begin
      dlgButton := TButton(aMsgDlg.Components[i]);
      if dlgButton.name = ButtonNames[Default] then
      dlgbutton.TabOrder := 0
    end;
  end;    
  Result := aMsgDlg.ShowModal;
  finally
  AmsgDlg.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  mymessagedialog('a',mtconfirmation,[mbno,mbyes,mbcancel,mbretry],mbcancel);
end;
end.

(you can wark more into to correct the taborder sequence...)

F68 ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9700572
windows.MessageBox(...
works also with d6, d5, d4, d3

d2, d1 i don't know

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9700579
seems to be a bit overdressed, f68,
since you could simple use the win-api,
but well, a working solution

;-)
0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 9700595
meikl i know it and i'd use messagebox instead, but he asked exactly:

"Q. Howto display MessageGld with default button set to mbNo (mbCancel...)?"

That's one :)))

F68 ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9700624
>That's one :)))

except you don't use messagedlg direct
-> specially you never use the messagedlg-call (just as my suggestion)

but well, no discussion about peanuts

meikl ;-)
0
 
LVL 2

Author Comment

by:odissey1
ID: 9706306
Hi  All,

Thank you for fast (overnight) reply!
I added extra 50 points and decided to split the total 100 between <kretzschmar> and <ferruccio68>. Solution by <kretzschmar> is straightforward.  Occasionally I put it as assisted answer, should be accepted one, sorry for my mistake. The disadvantage of Windows.Message(..) is that the message text is not centered (M$ job).
 
In example provided by ferruccio68 I dropped tweaking the buttons TAB order after ~2 hours of job. But this was very educational and thats why extra 50 points come out. Good thing is that I have only 2 buttons, so this would not be a problem, and the message text is centered.

Thank you All, and have a nice weekend!

Sincerely,
Boris


 



0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question