Einzelnen Beitrag anzeigen

Benutzerbild von KodeZwerg
KodeZwerg

Registriert seit: 1. Feb 2018
3.685 Beiträge
 
Delphi 11 Alexandria
 
#8

AW: Index des TMsgDlgButtons ermitteln

  Alt 23. Dez 2022, 13:31
Hier mal das was himitsu meinte und einer kleinen event-erweiterung
Delphi-Quellcode:
type
  TOnMsgDlgClick = procedure (Sender: TObject; const ATag: Integer) of object;
  TForm1 = class(TForm)
    ...
  strict private
  private
    procedure DlgClickEvent(Sender: TObject; const ATag: Integer);
  public
    FOnMsgDlgClick: TOnMsgDlgClick;
  public
    function MyMessageDlg(const sMsg: String; const aDlgTypt: TMsgDlgType; const sArrCaption: array of String; const sDlgCaption: String; var ClickEvent: TOnMsgDlgClick): Integer;
  public
    property ClickDlgEvent: TOnMsgDlgClick read FOnMsgDlgClick write FOnMsgDlgClick;
  end;

implementation

procedure TForm1.FormCreate(Sender: TObject);
begin
  FOnMsgDlgClick := DlgClickEvent;
end;

procedure TForm1.DlgClickEvent(Sender: TObject; const ATag: Integer);
begin
  // ab 1000 ist es ein button, alles darunter ist ein reguläres modal result, somit kannst du hier easy entscheiden was passieren soll bei einem button click
  // "case of" würde ich benutzen ...
  ShowMessage(IntToStr(ATag));
end;

function TForm1.MyMessageDlg(const sMsg: String; const aDlgTypt: TMsgDlgType; const sArrCaption: array of String; const sDlgCaption: String; var ClickEvent: TOnMsgDlgClick): Integer;
// *****************************************************************************************************************************************
var
  aMsgdlg: TForm;
  i: Integer;
  sButton: String;
  TaskDialog: TTaskDialog;
begin
  FOnMsgDlgClick := ClickEvent;
  // create a custom task dialog
  TaskDialog := TTaskDialog.Create(Application);
  try
    TaskDialog.Caption := sDlgCaption;
    TaskDialog.Title := sMsg;
    TaskDialog.CommonButtons := [];
    TaskDialog.ModalResult := 0;

    // add customized buttons to the task dialog
    for i := low(sArrCaption) to High(sArrCaption) do
    begin
      with TTaskDialogButtonItem(TaskDialog.Buttons.Add) do
      begin
        Caption := sArrCaption[i];
        ModalResult := i * 1000;
      end;
    end;

    // display the dialog box and read the ModalResult
    if TaskDialog.Execute then
      begin
        if Assigned(FOnMsgDlgClick) then
          FOnMsgDlgClick(Self, TaskDialog.ModalResult);
        Result := TaskDialog.ModalResult;
      end;
  finally
    TaskDialog.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  lDialogResult: Integer;
begin
  lDialogResult := MyMessageDlg('Test der eigenen Button.', mtConfirmation, ['ButtonYes', 'ButtonNo', 'ButtonOK', 'Cancel'], 'Dialog Test', FOnMsgDlgClick);
end;
Gruß vom KodeZwerg
  Mit Zitat antworten Zitat