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
Solved

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

Posted on 2003-11-06
13
1,204 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

856 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