[SOLVED] CreateMessageDialog - add default button option lazarus / 31-LCL ---------------------------------------------------------------------- petevick · 2022-02-13 18:08 I'm using CreateMessageDialog to create a custom message dialog that positions the dialog central on the main form, code as follows.... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function MyDialog(const aCaption: String;                  const aMsg: String;                  DlgType: TMsgDlgType;                  Buttons: TMsgDlgButtons;                  Center_X, Center_Y: integer): TModalResult;var f: TForm;begin  Result := 0;  f:=CreateMessageDialog(aMsg, DlgType, Buttons);  try    f.Caption:=aCaption;    f.Position:=poDesigned;    f.Left:=Center_X-f.Width div 2;    f.Top:=Center_Y-f.Height div 2;    Result:=f.ShowModal;  finally    f.Free;  end;end;I'd like to add a default button option the same as MessageDlg() has, would this be possible ?, I got as far as adding DefaultButton: TMsgDlgBtn to the function options as below.... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function MyDialog(const aCaption: String;                  const aMsg: String;                  DlgType: TMsgDlgType;                  Buttons: TMsgDlgButtons;                  Center_X, Center_Y: integer;                DefaultButton: TMsgDlgBtn): TModalResult;...and in the try section I was thinking of something like.... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     f.DefaultControl:=DefaultButton;.....but obviously that's wrong, and then my knowledge, talent and Google info ran out. ---------------------------------------------------------------------- wp · 2022-02-13 18:46 You probably are not aware of the QuestionDlg which is more flexible than the MessageDlg. You can specify buttons in any order with any caption and with any assosciated command, and with an arbitrary button marked as default or as cancel: https://lazarus- ccr.sourceforge.io/docs/lcl/dialogs/questiondlg.html Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  res: Integer;begin  res := QuestionDlg('Error', 'Do you really want to close this application?', mtConfirmation,    [mrYes, 'Yes,Sir',    // first button with optional caption "Yes, Sir" (could be skipped, then the caption would be "Yes"     mrNo, 'No, Sir', 'IsDefault', // second button with optional caption "No, Sir". Is the default button     99, 'Not sure'    // third button, returned modal result will have value 99.    ], 0);   if res = mrYes then    ShowMessage('Closing')  else    ShowMessage('Not closing');end; If you are accustomed to the syntax of MessageDlg you could use the QuestionDlg to write a simple modification of MessageDlg which allows to list the buttons in an arbitrary order and to specify the default button: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function MyMessageDlg(AMsg: String; DlgType: TMsgDlgType; Buttons: array of const; AHelpCtx: Integer): TModalResult;const  DialogIds : Array[mtWarning..mtCustom] of Longint = (idDialogWarning,   // requires LCLType in "uses"    idDialogError, idDialogInfo, idDialogConfirm, idDialogBase);begin  Result := QuestionDlg(GetDialogCaption(DialogIDs[DlgType]), AMsg, DlgType, Buttons, AHelpCtx);end; And above example would be called like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   res := MyMessageDlg('Do you really want to close this application?', mtConfirmation,    [mrYes, mrNo, 'IsDefault', 21, 'Not sure'], 0); ---------------------------------------------------------------------- jamie · 2022-02-13 19:00 How about the "MessageDlgPos" ? ---------------------------------------------------------------------- petevick · 2022-02-13 20:12 @wp I was aware of QuestionDlg, but not that it was so flexible.I very nearly settled for it, however, it doesn't have the X, Y position option. @jamie yes, I nearly settled for MessageDlgPos, but, no Caption option. and no default button option. ---------------------------------------------------------------------- petevick · 2022-02-14 12:03 possibly a step forward, I found similar to the following on a Delphi website, but I'm getting an Identifier not found "FindComponent"  error, I guess I can't use FindComponent in this situation. I've a feeling this is leading to a dead end. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     f.ActiveControl:=TWinControl(FindComponent('Cancel')); ---------------------------------------------------------------------- petevick · 2022-02-14 12:31 actually this is better in that I don't get any errors.... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     f.ActiveControl:=TWinControl(f.FindComponent('Cancel'));...but it is not making the Cancel button in the dialog the default. ---------------------------------------------------------------------- dseligo · 2022-02-14 12:59 Maybe I am missing something, but: why don't you create your own message dialog form and then you have everything under your control? ---------------------------------------------------------------------- balazsszekely · 2022-02-14 13:14 @petevick Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses Buttons; function MessageDlgEx(const AMsg: string; ADlgType: TMsgDlgType; AButtons: TMsgDlgButtons;  ADefButton: TBitBtnKind; AParent: TForm): TModalResult;var  MsgFrm: TForm;  I: Integer;begin  MsgFrm := CreateMessageDialog(AMsg, ADlgType, AButtons);  try    MsgFrm.FormStyle := fsStayOnTop;    for I := 0 to MsgFrm.ComponentCount - 1 do      if (MsgFrm.Components[I] is TBitBtn) then        if (MsgFrm.Components[I] as TBitBtn).Kind = ADefButton then          MsgFrm.ActiveControl := (MsgFrm.Components[I] as TBitBtn);    if AParent <> nil then    begin      MsgFrm.Position := poDefaultSizeOnly;      MsgFrm.Left := AParent.Left + (AParent.Width - MsgFrm.Width) div 2;      MsgFrm.Top := AParent.Top + (AParent.Height - MsgFrm.Height) div 2;    end    else      MsgFrm.Position := poWorkAreaCenter;    Result := MsgFrm.ShowModal;  finally    MsgFrm.Free  end;end;  procedure TForm1.Button1Click(Sender: TObject);begin  //bkCustom, bkOK, bkCancel, bkHelp, bkYes, bkNo, bkClose, bkAbort, bkRetry, bkIgnore, bkAll, bkNoToAll, bkYesToAll  if MessageDlgEx('Test test test! Cancel button is focused.', mtConfirmation, [mbYes, mbNo, mbCancel], bkCancel, Self) = mrCancel then    ShowMessage('Canceled.')end;   QuoteI'm using CreateMessageDialog to create a custom message dialog that positions the dialog central on the main form, code as follows..The last parameter of MessageDlgEx is a form, the dialog will be central to this form. In your case, you should pass the mainform. ---------------------------------------------------------------------- petevick · 2022-02-14 15:23 @GetMem Wow GetMem, that is fantastic   , I would never have got to that. I've added const aCaption: String; and MsgFrm.Caption:=aCaption; and that gives me everything I've been after. Can't thank you enough for this