Recent

Author Topic: How to insert a form in a unit and have the IDE manage it?  (Read 925 times)

jamie

  • Hero Member
  • *****
  • Posts: 6791
How to insert a form in a unit and have the IDE manage it?
« on: October 15, 2024, 09:57:39 pm »

 I have question about later deciding to insert a Form in a UNIT that got started without any interacting with the IDE designer.

Is it possible to insert a form class into an existing unit and have it be managed by the IDE/OI ?

I don't mean in forms of editing the LFM file, is there an option to start a new form within an existing unit?
The only true wisdom is knowing you know nothing

MarkMLl

  • Hero Member
  • *****
  • Posts: 8111
Re: How to insert a form in a unit and have the IDE manage it?
« Reply #1 on: October 15, 2024, 10:22:39 pm »
What happens if you add the relevant .pas file to the project?

I admit to being somewhat uncomfortable with this distinction between "form" and "unit", which is something that I've seen people making repeatedly (https://forum.lazarus.freepascal.org/index.php/topic,65825.0.html and so on). The important thing is the unit and the class (descendant of TForm) defined therein in the context of Object Pascal: the IDE should take note of the presence of that and (a) compile the associated .lfm into a collection of resources and (b) use those resources to populate a form at program startup.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

jamie

  • Hero Member
  • *****
  • Posts: 6791
Re: How to insert a form in a unit and have the IDE manage it?
« Reply #2 on: October 15, 2024, 10:54:03 pm »
That's just it, I don't have a Form already in the process, I have a unit that I would like to have a visiual editable form in it now.

I just put in
TMYFORM = Class(TFORM)
...
etc.

I don't have any options for INSERT a form instead of starting a new form which starts a new empty unit with the form.

Also, I would like to know if creating Frames can be held to the current project and not installed on the IDE bar?
The only true wisdom is knowing you know nothing

MarkMLl

  • Hero Member
  • *****
  • Posts: 8111
Re: How to insert a form in a unit and have the IDE manage it?
« Reply #3 on: October 15, 2024, 11:24:00 pm »
That's just it, I don't have a Form already in the process, I have a unit that I would like to have a visiual editable form in it now.
I don't have any options for INSERT a form instead of starting a new form which starts a new empty unit with the form.

Tell the IDE to open the .pas file and then make sure it's added to the project. If that doesn't work then I suspect we need Martin, as the IDE guru, in here.

Quote
Also, I would like to know if creating Frames can be held to the current project and not installed on the IDE bar?

A frame is entirely in the local project. It's just a control instantiated onto the current form, and isn't in some way added to the IDE.

(Noting the repeated warnings against trying to edit a frame after it's been instantiated, which invariably ends badly.)

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

jamie

  • Hero Member
  • *****
  • Posts: 6791
Re: How to insert a form in a unit and have the IDE manage it?
« Reply #4 on: October 16, 2024, 12:11:28 am »
I need to make a ToolBar type controls and the main form is getting large where these toolbars are going to be used.

So I want to make each toolbar type control in different forms because these toolbars are also basically like a form with no caption border etc., they have properties and methods etc.

  I have no intention of dropping them on the form at design time, these will be created and parented to the control housing the toolbars. these bars will also perform floating and docking operations.

 Current project I am converting over has many of these that are more than just the toolbar control, they have all sorts of fields that are relevant only to the project, they need to be created at startup and keep that way, regardless of how they are being accessed visually.

 Thank you.

The only true wisdom is knowing you know nothing

Sieben

  • Sr. Member
  • ****
  • Posts: 372
Re: How to insert a form in a unit and have the IDE manage it?
« Reply #5 on: October 16, 2024, 01:12:15 am »
Maybe you're looking for sth like this - from my own version but essentially code from unit Dialogs. Creating a TForm code only:

Code: Pascal  [Select][+][-]
  1. type
  2.  
  3.   { TaxInputForm }
  4.  
  5.   TaxInputForm = class(TForm)
  6.   private
  7.     Edit: TCustomEdit;
  8.     FNonChars: TCharSet;
  9.     procedure InputEditChange(Sender: TObject);
  10.     procedure InputEditKeyPress(Sender: TObject; var Key: Char);
  11.   protected
  12.     procedure Activate; override;
  13.   end;
  14.  
  15. // + the implementations
  16.  
  17. function axCreateInputDialog(OwnerForm: TCustomForm;
  18.                              const ACaption, APrompt: string;
  19.                              MaskInput: Boolean; NonChars: TCharSet;
  20.                              const AMask: string; AEditClass: TEditClass;
  21.                              var Value: string): TaxInputForm;
  22. var Prompt: TLabel;
  23.     MinEditWidth: integer;
  24.     AMonitor: TMonitor;
  25. begin
  26.   Result := nil;
  27.   Result := TaxInputForm(TaxInputForm.NewInstance);
  28.   Result.DisableAutoSizing;
  29.   Result.CreateNew(OwnerForm);
  30.   with Result do
  31.   begin
  32.     FNonChars := NonChars;
  33.     PopupMode := pmAuto;
  34.     BorderStyle := bsDialog;
  35.     Caption := ACaption;
  36.     Prompt := TLabel.Create(Result);
  37.     with Prompt do
  38.     begin
  39.       Parent := Result;
  40.       Caption := APrompt;
  41.       Align := alTop;
  42.       AutoSize := True;
  43.     end;
  44.     Edit := AEditClass.Create(Result);
  45.     with Edit do
  46.     begin
  47.       Parent := Result;
  48.       Top := Prompt.Height;
  49.       Align := alTop;
  50.       BorderSpacing.Top := 3;
  51.       AMonitor := _InputQueryActiveMonitor;
  52.       // check that edit is smaller than our monitor, it must be smaller at least
  53.       // by 6 * 2 pixels (spacing from window borders) + window border
  54.       MinEditWidth := Min(AMonitor.Width - 20,
  55.                       Max(cInputQueryEditSizePixels,
  56.                       AMonitor.Width * cInputQueryEditSizePercents div 100));
  57.       Constraints.MinWidth := MinEditWidth;
  58.       OnKeyPress := @Result.InputEditKeyPress;
  59.       OnChange := @Result.InputEditChange;
  60.       {$ifdef with_picedits}
  61.       if (AEditClass = TPxPicEdit) then
  62.         TPxPicEdit(Edit).ValueConstraints.PictureMask := AMask;
  63.       {$endif}
  64.       if (AEditClass = TMaskEdit) then
  65.         TMaskEdit(Edit).EditMask := AMask;
  66.       if (Value > '') then Text := Value;
  67.       TabStop := True;
  68.       if MaskInput then
  69.       begin
  70.         EchoMode := emPassword;
  71.         PasswordChar := '*';
  72.       end else
  73.       begin
  74.         EchoMode := emNormal;
  75.         PasswordChar := #0;
  76.       end;
  77.       TabOrder := 0;
  78.     end;
  79.  
  80.     with TaxDlgBtnPnl.Create(Result) do
  81.     begin
  82.       Top := Edit.Top + Edit.Height;
  83.       Parent := Result;
  84.       ShowBevel := False;
  85.       ShowButtons := [pbOK, pbCancel];
  86.       Align := alTop;
  87.     end;
  88.  
  89.     ChildSizing.TopBottomSpacing := 6;
  90.     ChildSizing.LeftRightSpacing := 6;
  91.     AutoSize := True;
  92.  
  93.     InputEditChange(Edit);
  94.  
  95.     // upon show, the edit control will be focused for editing, because it's
  96.     // the first in the tab order
  97.     Result.EnableAutoSizing;
  98.   end;
  99. end;
  100.  
  101.  

IDE/OI, however, will of course not manage this.
« Last Edit: October 16, 2024, 01:49:15 am by Sieben »
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

LV

  • Full Member
  • ***
  • Posts: 195
Re: How to insert a form in a unit and have the IDE manage it?
« Reply #6 on: October 16, 2024, 04:28:31 pm »

 I have question about later deciding to insert a Form in a UNIT that got started without any interacting with the IDE designer.

Is it possible to insert a form class into an existing unit and have it be managed by the IDE/OI ?

I don't mean in forms of editing the LFM file, is there an option to start a new form within an existing unit?

The answer is yes if the conditions are met:
1) the *.lfm file must be created, otherwise the Object Inspector will not see the form;
2) You need to add a line to the *.lpr (Application.CreateForm(...););
I spent 3 mins checking it out.

What happens if you add the relevant .pas file to the project?

I admit to being somewhat uncomfortable with this distinction between "form" and "unit", which is something that I've seen people making repeatedly (https://forum.lazarus.freepascal.org/index.php/topic,65825.0.html and so on). The important thing is the unit and the class (descendant of TForm) defined therein in the context of Object Pascal: the IDE should take note of the presence of that and (a) compile the associated .lfm into a collection of resources and (b) use those resources to populate a form at program startup.

MarkMLl

I'm of the same opinion. I will add a discussion close to this issue
https://forum.lazarus.freepascal.org/index.php/topic,68574.0.html

 

TinyPortal © 2005-2018