2009. október 12., hétfő

How to change the default button in a MessageDlg


Problem/Question/Abstract:

How to change the default button in a MessageDlg

Answer:

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
    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;
    Result := ShowModal;
  finally
    Free;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if DefMessageDlg('Please confirm', 'Do you want to format your harddisk now?', mtConfirmation, mbYesNoCancel, mrno, 0) = mrYes then
    ShowMessage('Formatting disk...');
end;

Nincsenek megjegyzések:

Megjegyzés küldése