?
Solved

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

Posted on 2003-11-06
13
Medium Priority
?
1,257 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month8 days, 15 hours left to enroll

764 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