Lazarus

Programming => General => Topic started by: Bazzao on March 13, 2019, 11:11:28 pm

Title: Dynamic number of buttons for QuestionDlg
Post by: Bazzao on March 13, 2019, 11:11:28 pm
On page
http://wiki.freepascal.org/Dialog_Examples

There are function references for MessageDLG and other dialog boxes, but not QuestionDlg.

In my application, I need two versions of a QuestionDlg, because 1 has an additional button.

Q1: Instead of hard coding, can I make the button array [] dynamic (ie add remove buttons (value and string))?

Q2: Can I make the 'IsDefault' attribute dynamic (ie move it to another value/string pair)?

TIA

Bazza
Title: Re: Dynamic number of buttons for QuestionDlg
Post by: Blaazen on March 14, 2019, 12:47:47 am
Ad 1): IMO not with array of const. You can use case..of or if..then but you must hardcode parameters.
Code: Pascal  [Select][+][-]
  1. if a=1
  2.   then QuestionDlg('Cap', 'Msg', mtInformation, [mrCancel, mrOK], '')
  3.   else QuestionDlg('Cap', 'Msg', mtInformation, [mrNo, mrYes], '');
Title: Re: Dynamic number of buttons for QuestionDlg
Post by: Bazzao on March 14, 2019, 07:41:28 am
Thanks Blaazen. I thought that might be the case. Silly hard coding.
Title: Re: Dynamic number of buttons for QuestionDlg
Post by: ASerge on March 14, 2019, 10:02:53 pm
Ad 1): IMO not with array of const.
Also possible.
Code: Pascal  [Select][+][-]
  1. type
  2.   TArrayOfConst = array of TVarRec;
  3.  
  4. function CreateArrayOfConst(const A: array of const): TArrayOfConst;
  5. var
  6.   i: SizeInt;
  7. begin
  8.   SetLength(Result, Length(A));
  9.   for i := Low(A) to High(A) do
  10.     Result[i] := A[i];
  11. end;
  12.  
  13. operator + (const A, B: TArrayOfConst): TArrayOfConst;
  14. var
  15.   i, Shift: SizeInt;
  16. begin
  17.   SetLength(Result, Length(A) + Length(B));
  18.   for i := Low(A) to High(A) do
  19.     Result[i] := A[i];
  20.   Shift := Length(A);
  21.   for i := Low(B) to High(B) do
  22.     Result[i + Shift] := B[i];
  23. end;
  24.  
  25. procedure TForm1.FormCreate(Sender: TObject);
  26. var
  27.   Buttons: TArrayOfConst;
  28. begin
  29.   Buttons := CreateArrayOfConst([mrYes, mrNo]);
  30.   if 2 > 1 then
  31.     Buttons := Buttons + CreateArrayOfConst(['IsDefault']);
  32.   QuestionDlg('Caption', 'Message', mtInformation, Buttons, 0);
  33. end;
TinyPortal © 2005-2018