Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic dialog with custom captions for buttons

I know this issue have been up since before (ex. Best way to show customized message dialogs), but I still don't find what I want.

I started like this:

class function TAttracsForm.MessageDlg(const aMsg: string; aDlgType: TMsgDlgType; Buttons: TMsgDlgButtons; aCaptions: array of String; aDefault: TMsgDlgBtn): TModalResult;
var
  vDlg: TForm;
  i: Integer; 
begin
  if aButtons.Count = aCaptions.Count then
  begin
    vDlg := CreateMessageDialog(aMsg, aDlgType, Buttons);
    try
      for i := 0 aCaptions.Count - 1 do
        TButton(vDlg.FindComponent(Buttons[i].Caption)).Caption := aCaptions[i]; 

      vDlg.Position := poDefaultPosOnly;
      Result := vDlg.ShowModal;
    finally
      vDlg.Free;
    end;
  end;
end;

And the call would look like:

if (MessageDlg('Really quit application ?', mtWarning, 
       [mbNo, mbCancel, mbYes], {'No save', 'Cancel', 'Save'}) = mrYes) then

But the above code of course don't compile. I don't know how to get one item of an set in the loop and how to get the total count of it in the beginning.

like image 404
Roland Bengtsson Avatar asked Mar 24 '11 10:03

Roland Bengtsson


3 Answers

you can use this code:

function MyMessageDlg(CONST Msg: string; DlgTypt: TmsgDlgType; button: TMsgDlgButtons;
  Caption: ARRAY OF string; dlgcaption: string): Integer;
var
  aMsgdlg: TForm;
  i: Integer;
  Dlgbutton: Tbutton;
  Captionindex: Integer;
begin
  aMsgdlg := createMessageDialog(Msg, DlgTypt, button);
  aMsgdlg.Caption := dlgcaption;
  aMsgdlg.BiDiMode := bdRightToLeft;
  Captionindex := 0;
  for i := 0 to aMsgdlg.componentcount - 1 Do
  begin
    if (aMsgdlg.components[i] is Tbutton) then
    Begin
      Dlgbutton := Tbutton(aMsgdlg.components[i]);
      if Captionindex <= High(Caption) then
        Dlgbutton.Caption := Caption[Captionindex];
      inc(Captionindex);
    end;
  end;
  Result := aMsgdlg.Showmodal;
end;

For example:

MyMessageDlg('Hello World!', mtInformation, [mbYes, mbNo],
      ['Yessss','Noooo'], 'New MessageDlg Box'):
like image 194
Mohammad Avatar answered Oct 24 '22 09:10

Mohammad


How about something like this:

type
  TButtonInfo = record
    MsgDlgBtn: TMsgDlgBtn;
    Caption: string;
  end;

function ButtonInfo(MsgDlgBtn: TMsgDlgBtn; const Caption: string): TButtonInfo;
begin
  Result.MsgDlgBtn := MsgDlgBtn;
  Result.Caption := Caption;
end;

const
  ModalResults: array[TMsgDlgBtn] of Integer = (
    mrYes, mrNo, mrOk, mrCancel, mrAbort, mrRetry, mrIgnore, mrAll, mrNoToAll,
    mrYesToAll, 0, mrClose);

function FindDialogButton(Form: TForm; MsgDlgBtn: TMsgDlgBtn): TButton;
var
  i: Integer;
  Component: TComponent;
begin
  for i := 0 to Form.ComponentCount-1 do begin
    Component := Form.Components[i];
    if Component is TButton then begin
      if TButton(Component).ModalResult=ModalResults[MsgDlgBtn] then begin
        Result := TButton(Component);
        exit;
      end;
    end;
  end;
  Result := nil;
end;

function MessageDlg(
  const aMsg: string;
  aDlgType: TMsgDlgType;
  const Buttons: array of TButtonInfo;
  aDefault: TMsgDlgBtn
): TModalResult;
var
  i: Integer;
  MsgDlgButtons: TMsgDlgButtons;
  vDlg: TForm;
begin
  MsgDlgButtons := [];
  for i := low(Buttons) to high(Buttons) do begin
    Assert(not (Buttons[i].MsgDlgBtn in MsgDlgButtons));//assert uniqueness
    Include(MsgDlgButtons, Buttons[i].MsgDlgBtn);
  end;
  vDlg := CreateMessageDialog(aMsg, aDlgType, MsgDlgButtons, aDefault);
  try
    for i := low(Buttons) to high(Buttons) do begin
      FindDialogButton(vDlg, Buttons[i].MsgDlgBtn).Caption := Buttons[i].Caption;
    end;
    vDlg.Position := poDefaultPosOnly;
    Result := vDlg.ShowModal;
  finally
    vDlg.Free;
  end;
end;

procedure Test;
begin
  MessageDlg(
    'Really quit application ?',
    mtWarning,
    [ButtonInfo(mbNo, 'Do&n''t save'), ButtonInfo(mbCancel, '&Cancel'), ButtonInfo(mbYes,'&Save')],
    mbYes
  );
end;

enter image description here

like image 30
David Heffernan Avatar answered Oct 24 '22 10:10

David Heffernan


I write this code: (I am from Croatia, so the texts are in Croatian)

function MojDijalog(const Msg, Capt: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons;
  DefaultButton: TMsgDlgBtn): TModalResult;
var
  dlg : TForm;
begin
  dlg := CreateMessageDialog(Msg, DlgType, Buttons, DefaultButton) ;
  with dlg do begin
     Caption := Capt;
     FormStyle := fsStayOnTop;
     ( FindComponent( 'OK' ) as TButton ).Caption := 'U redu' ;
     ( FindComponent( 'Cancel' ) as TButton ).Caption := 'Odustani' ;
     ( FindComponent( 'Yes' ) as TButton ).Caption := 'Da' ;
     ( FindComponent( 'No' ) as TButton ).Caption := 'Ne' ;
      ( FindComponent( 'Help' ) as TButton ).Caption := 'Pomoć' ;
     ( FindComponent( 'Close' ) as TButton ).Caption := 'Zatvori' ;
     ( FindComponent( 'Ignore' ) as TButton ).Caption := 'Zanemari' ;
     ( FindComponent( 'Retry' ) as TButton ).Caption := 'Pokušaj ponovo' ;
     ( FindComponent( 'Abort' ) as TButton ).Caption := 'Prekini' ;
     ( FindComponent( 'All' ) as TButton ).Caption := 'Sve' ;
  end;
  Result := dlg.ShowModal;
end;

Example of use :

if MojDijalog('Obrisati zapis ?','Upit za brisanje',mtConfirmation,mbYesNo,mbNo) = mrNo then
    begin
         Abort;
    end;
like image 2
Alan Milanović Avatar answered Oct 24 '22 09:10

Alan Milanović