delphi Creating Component template
Asked Answered
I

2

1

I am working with Delphi application.I created one form shown as below:

enter image description here

I wanted to make component out of this controls through code. But not through component-->create component Template-->so on.

How do i make component template out of form contols through delphi code.?? Thanx in advance.

Irvinirvine answered 28/6, 2012 at 11:1 Comment(1)
This question is unclear at this time. Do you want to create the components from your screenshot at runtime through code ? Or do you want to create a component group which you would be able to add to your form as one component ? Or do you want to save this group of components (as a frame) and reuse this group many times in your project(s) ?Caballero
C
5

Or if you want to have that group of controls as one single component you can install unit like this into some package:

unit EditGroup;

interface

uses
  SysUtils, Classes, Graphics, Controls, StdCtrls;

type
  TEditGroup = class(TCustomControl)
  private
    FButton: TButton;
    FFirstEdit: TEdit;
    FFirstLabel: TLabel;
    FSecondEdit: TEdit;
    FSecondLabel: TLabel;
  protected
    procedure Paint; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Button: TButton read FButton;
    property FirstEdit: TEdit read FFirstEdit;
    property FirstLabel: TLabel read FFirstLabel;
    property SecondEdit: TEdit read FSecondEdit;
    property SecondLabel: TLabel read FSecondLabel;
  end;

procedure Register;

implementation

{ TEditGroup }

constructor TEditGroup.Create(AOwner: TComponent);
begin
  inherited;

  Width := 213;
  Height := 104;
  Color := clWhite;

  FFirstLabel := TLabel.Create(Self);
  FFirstLabel.SetSubComponent(True);
  FFirstLabel.Parent := Self;
  FFirstLabel.Top := 11;
  FFirstLabel.Left := 8;
  FFirstLabel.Name := 'FirstLabel';

  FFirstEdit := TEdit.Create(Self);
  FFirstEdit.SetSubComponent(True);
  FFirstEdit.Parent := Self;
  FFirstEdit.Top := 8;
  FFirstEdit.Left := 84;
  FFirstEdit.Width := 121;
  FFirstEdit.Name := 'FirstEdit';

  FSecondLabel := TLabel.Create(Self);
  FSecondLabel.SetSubComponent(True);
  FSecondLabel.Parent := Self;
  FSecondLabel.Top := 39;
  FSecondLabel.Left := 8;
  FSecondLabel.Name := 'SecondLabel';

  FSecondEdit := TEdit.Create(Self);
  FSecondEdit.SetSubComponent(True);
  FSecondEdit.Parent := Self;
  FSecondEdit.Top := 36;
  FSecondEdit.Left := 84;
  FSecondEdit.Width := 121;
  FSecondEdit.Name := 'SecondEdit';

  FButton := TButton.Create(Self);
  FButton.SetSubComponent(True);
  FButton.Parent := Self;
  FButton.Top := 71;
  FButton.Left := 69;
  FButton.Width := 75;
  FButton.Name := 'Button';
end;

destructor TEditGroup.Destroy;
begin
  FButton.Free;
  FFirstEdit.Free;
  FFirstLabel.Free;
  FSecondEdit.Free;
  FSecondLabel.Free;
  inherited;
end;

procedure TEditGroup.Paint;
begin
  Canvas.Rectangle(ClientRect);
end;

procedure Register;
begin
  RegisterComponents('Stack Overflow', [TEditGroup]);
end;

end.

Here's how it looks like at design time:

enter image description here

Caballero answered 28/6, 2012 at 20:44 Comment(3)
+1 For nicely demonstrating how to make this a customizable component @designtime.Corroborant
@NGLN, thanks! But I think OP wants what you've described (and what I've already upvoted).Caballero
oh, I've been searching for something like that and now I finally find it! really good example!Colophony
C
3

If you right-click on the form and choose View as Text, then you are already a long way. Simply replace all ='s by :='s, and create all components by adding .Create(Self).

So this text:

object Form1: TForm1
  Left = 300
  Top = 281
  Width = 630
  Height = 372
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 14
    Top = 28
    Width = 32
    Height = 13
    Caption = 'Label1'
  end
  object Edit1: TEdit
    Left = 63
    Top = 24
    Width = 121
    Height = 21
    TabOrder = 0
    Text = 'Edit1'
  end
end

should be converted into something like:

type
  TMyForm1 = class(TForm)
  private
    Label1: TLabel;
    Edit1: TEdit;
  public
    constructor Create(AOwner: TComponent); override;
  end;

constructor TMyForm1.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Width := 630;
  Height := 372;
  Caption := 'Form1';
  Color := clBtnFace;
  ...
  Label1 := TLabel.Create(Self);
  with Label1 do
  begin
    Left := 14;
    Top := 28;
    Width := 32;
    Height := 13;
    Caption := 'Label1';
  end;
  Edit1 := TEdit.Create(Self);
  with Edit1 do
  ...
end;

But there are also tools for this special task, see Are there any Delphi DFM to Delphi source code convertion tools?.

Corroborant answered 28/6, 2012 at 20:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.