Solved

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

Posted on 2003-11-06
13
1,196 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
  • 6
  • 3
  • 2
  • +2
13 Comments
 
LVL 27

Assisted Solution

by:kretzschmar
kretzschmar earned 50 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 22

Accepted Solution

by:
Ferruccio Accalai earned 50 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 22

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
problem when i try to pack my dll file with upx 9 72
Downloading email attachments 2 67
Making delphi communicate with a c# service 16 91
Working with hours 3 46
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 my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

861 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

Need Help in Real-Time?

Connect with top rated Experts

30 Experts available now in Live!

Get 1:1 Help Now