Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1763
  • Last Modified:

MessageDlg with a DefaultButton ?

Hi Folks,

I don't know what is wrong, but my memory seems to be troubling me.  I need to display a message dialog with a mbYes, mbNo, and mbYesToAll button.  So I use the MessageDlg function for that, which is perfect.  But somehow this function always uses the mbYes as the default button, and of course I need the mbNo button to be the default one.  I thought this was possible, that there was an overloaded function in which you could pass the DefaultButton, but apparently I am wrong.

Does anyone have a simple solution for this ?


Regards,
0
Stefaan
Asked:
Stefaan
  • 3
  • 3
1 Solution
 
kretzschmarCommented:
you could use the windows.messagebox
( also the buttons may than in a localized language)

like

  if windows.MessageBox( self.Handle
                       , 'AText'
                       , 'ACaption'
                       , mb_iconwarning or
                         mb_yesnocancel or
                         mb_defbutton2)  = mryes then  //default by NO

meikl ;-)
0
 
DaFoxCommented:
Hi Stefaan.

You can't do this with a MessageDlg, Borland forgot about that ;-). The problem is that they don't use the standard Windows dialog boxes (they're using their own dialog instead).

You have to do it with MessageBox instead (or CreateMessageDlg; see below)

Here's a nice example made by Peter Below:

{+------------------------------------------------------------
 | Function DefMessageDlg
 |
 | Parameters:
 |  aCaption: Caption to use for the dialog. If empty the
 |            default is used.
 |  Msg     : message to display
 |  Dlgtype : type of dialog, see MessageDlg online help
 |  Buttons : buttons to display, see MessageDlg online help
 |  Defbutton: ModalResult of the button that should be the
 |             default.
 |  HelpCtx:  help context (optional)
 | Returns:
 |  ModalResult of the dialog
 | Description:
 |  This is a wrapper around CreateMessageDialog, basically
 |  an extension of the MessageDlg VCL function that allows
 |  us to modify the caption and determine which button will
 |  be the default.
 | Error Conditions:
 |  none
 |
 |Created: 07.06.98 14:54:20 by P. Below
 +------------------------------------------------------------}
Function DefMessageDlg(const aCaption: String;
                       const Msg: string;
                       DlgType: TMsgDlgType;
                       Buttons: TMsgDlgButtons;
                       DefButton: Integer;
                       HelpCtx: Longint): Integer;
Var
  i: Integer;
  btn: TButton;
Begin
  With CreateMessageDialog(Msg, DlgType, Buttons) Do
  try
    If Length(aCaption) > 0 Then
      Caption := aCaption;
    HelpContext := HelpCtx;
    For i := 0 To ComponentCount-1 Do Begin
      If Components[i] Is TButton Then Begin
        btn := TButton(Components[i]);
        btn.Default:= btn.ModalResult = DefButton;
        If btn.Default Then
          ActiveControl := Btn;
      End;
    End; { For }
    Result := ShowModal;
  finally
    Free;
  end;
End;


Markus
0
 
DaFoxCommented:
Argh!

May I quote Madshi?
"I won't get meikl, anyway, he's just too fast for me...   :-)"

;-)

Markus
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
kretzschmarCommented:
:-)))

ah, dafox,
I'm really silent the last days,
don't be frustrated, my quickness
is only randomly

btw. you gave a nice alternative

meikl ;-)
0
 
StefaanAuthor Commented:
Exactly what I was searching for.  And I knew Borland had somthing like this, but apparently thats in the CLX from delphi 7 ( QDialogs unit ).

Thanks for the help.,
0
 
kretzschmarCommented:
you see, dafox, quickness is not all :-))
0
 
DaFoxCommented:
Yep, "Gut' Ding will Weile haben" ;-)

Markus
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now