Solved

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

Posted on 2003-11-06
13
1,187 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
Comment Utility
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
Comment Utility
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
Comment Utility
hi,

Did you use the Dialogs unit for your form?

HTH,

Andrew
0
 
LVL 27

Expert Comment

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

Expert Comment

by:DeerBear
Comment Utility
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
Comment Utility
>Just remove the second mbYes and it should work.
then you have no control about the default-button
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 5

Expert Comment

by:DeerBear
Comment Utility
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
Comment Utility
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
Comment Utility
windows.MessageBox(...
works also with d6, d5, d4, d3

d2, d1 i don't know

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
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
Comment Utility
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
Comment Utility
>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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

763 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

7 Experts available now in Live!

Get 1:1 Help Now