Recent

Author Topic: How to create a TForm dynamically  (Read 3390 times)

rc.1990

  • Jr. Member
  • **
  • Posts: 54
How to create a TForm dynamically
« on: September 23, 2016, 07:32:45 pm »
Is it possible to create a TForm dynamically without having the files .pas and .lfm?
If possible, I am trying to do something similar to inputbox or showmessage or messagedlg, but including a TPanel and a TImage and some more Labels and Edits.
« Last Edit: September 23, 2016, 07:35:10 pm by rc.1990 »

djzepi

  • New Member
  • *
  • Posts: 36

rc.1990

  • Jr. Member
  • **
  • Posts: 54
Re: How to create a TForm dynamically
« Reply #2 on: September 23, 2016, 08:16:51 pm »
Thanks.

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: How to create a TForm dynamically
« Reply #3 on: September 23, 2016, 08:42:40 pm »
Although djzepi was faster, here's an advanced example in which a dialog with a radiogroup is created dynamically; the example makes heavy use of the autosizing properties available in Lazarus:
Code: Pascal  [Select][+][-]
  1. function RadioButtonDialog(ACaption, Alabel: String; const AItems: array of string;
  2.   var AItemIndex: Integer): Boolean;
  3. var
  4.   F: TForm;
  5.   rg: TRadioGroup;
  6.   i: Integer;
  7.   cancelBtn, okBtn: TButton;
  8.   w, h: Integer;
  9. begin
  10.   F := TForm.Create(nil);
  11.   try
  12.     F.Caption := ACaption;
  13.  
  14.     // Create the radiogroup
  15.     rg := TRadioGroup.Create(F);
  16.     rg.Parent := F;
  17.     // Set caption of radiogroup
  18.     rg.Caption := ALabel;
  19.     // Create items of radiogroup
  20.     for i:=0 to High(AItems) do
  21.       rg.Items.Add(AItems[i]);
  22.     // Select the predefined item
  23.     rg.ItemIndex := AItemIndex;
  24.     // Automatically determine size of radiogroup
  25.     rg.BorderSpacing.Around := 8;
  26.     rg.AutoSize := true;
  27.     rg.HandleNeeded;
  28.     rg.GetPreferredSize(w, h);
  29.     rg.AutoSize := false;
  30.     rg.Align := alTop;
  31.     rg.Height := h;
  32.  
  33.     // Create the OK button
  34.     okBtn := TButton.Create(F);
  35.     okBtn.Parent := F;
  36.     okBtn.Caption := 'OK';
  37.     okBtn.ModalResult := mrOK;
  38.     okBtn.Default := true;
  39.  
  40.     // Create the Cancel button
  41.     cancelBtn := TButton.Create(F);
  42.     cancelBtn.parent := F;
  43.     cancelBtn.BorderSpacing.Around := 8;
  44.     cancelBtn.Caption := 'Cancel';
  45.     cancelBtn.Cancel := true;
  46.     cancelBtn.ModalResult := mrCancel;
  47.  
  48.     // Automatically position the cancel button underneath the radiogroup and
  49.     // anchor it to the right of the form
  50.     cancelBtn.AnchorSideTop.Control := rg;
  51.     cancelBtn.AnchorSideTop.Side := asrBottom;
  52.     cancelBtn.Anchors := [akTop, akRight];
  53.     cancelBtn.AnchorSideRight.Control := F;
  54.     cancelBtn.AnchorSideRight.Side := asrBottom;
  55.  
  56.     // Automatically position the OK button to the left of the cancel button
  57.     okBtn.BorderSpacing.Around := 8;
  58.     okbtn.AnchorSideTop.Control := rg;
  59.     okbtn.AnchorSideTop.Side := asrBottom;
  60.     okbtn.Anchors := [akTop, akRight];
  61.     okbtn.AnchorSideRight.Control := cancelbtn;
  62.     okbtn.AnchorSideRight.Side := asrLeft;
  63.  
  64.     // Automatically determine best size of the form
  65.     F.AutoSize := true;
  66.  
  67.     // Position the form in the screen center
  68.     F.Position := poScreenCenter;
  69.  
  70.     // Show form modally
  71.     if F.ShowModal = mrOK then begin
  72.       Result := true;
  73.       AItemIndex := rg.ItemIndex;
  74.     end else
  75.       Result := false;
  76.   finally
  77.     F.Free;
  78.   end;
  79. end;
  80.  
  81. procedure TForm1.Button1Click(Sender: TObject);
  82. var
  83.   sel: Integer;
  84. begin
  85.   sel := 0;
  86.   if RadioButtonDialog(
  87.     'Please select',
  88.     'Options',
  89.     ['1st item', '2nd item', '3rd item', '4th item - very, very, very long text',
  90.      '5th item', '6th item', '7th item'],
  91.      sel)
  92.   then
  93.     ShowMessage('Item ' + IntToStr(sel) + ' selected');
  94. end;

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How to create a TForm dynamically
« Reply #4 on: September 23, 2016, 09:15:33 pm »
And attached is a similar sort of example that includes a panel, image, label and edit.

rc.1990

  • Jr. Member
  • **
  • Posts: 54
Re: How to create a TForm dynamically
« Reply #5 on: September 24, 2016, 12:01:08 am »
Thanks, wp and howardpc.

 

TinyPortal © 2005-2018