Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
Member Area
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Become a Member
-Why sign up!
-Chat Online!
-Indexes NEW!!
-Build your resume
-Find a job
-Post a job
-Resume Search
-Link to us
Visit Embarcadero
Embarcadero Community
How to create a subform control Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Delphi 3.x
User Rating
No Votes
# Votes
DSP, Administrator
Reference URL:
			Author: Jonas Bilinkevicius

How to create a subform control


Those programmers who use the Win API in their programs know that Win32 allows you 
to insert one dialog box into another one and you'll can deal with subdialog's 
controls as them were in parent dialog. The good example of it is PropertySheet. I 
don't know why Borland hided this ability from us and why didn't it insert 
'subforming' ability in TForm control. Here I can tell how to use a form as control 
(subform) in other one and how to create subform controls. It will work in D2, D3 
and may be D4 (unfortunatelly, I have not it and can't check). The next steps shows 
how to make subform component:

First, we have to make the form to be a child. For this we need to override the 
method CreateParams.

1   type
2     TSubForm = class(TForm)
3     protected
4       procedure CreateParams(var Params: TCreateParams); override;
5     end;
7   procedure TSubForm.CreateParams(var Params: TCreateParams);
8   begin
9     inherited CreateParams(Params);
10    Params.Style := WS_CHILD or WS_DLGFRAME or WS_VISIBLE or DS_CONTROL;
11  end;

It's enough if you will not register this control into Delphi IDE. Now you can 
insert TSubForm control into a form at run time as shown below:

12  { ... }
13  with TSubForm.Create(YourForm) do
14  begin
15    Parent := YourForm;
16    Left := 8;
17    Top := 8;
18  end;
19  { ... }

Unfortunately, it's not enough if you want insert this control into Delphi IDE. You 
have to do next two important things for it. Override TSubForm's destructor for 
prevent Delphi from break when subform will be deleted at design time (by user or 
Delphi). It can be fixed with next code:

20  destructor TSubForm.Destroy;
21  begin
22    SetDesigning(False);
23    inherited Destroy;
24  end;

Now your subform (sure inserted into form) looks like gray rectangle. The good deal 
is to make subform to show it's components at design time:

25  constructor TSubForm.Create(aOwner: TComponent);
26  begin
27    inherited Create(aOwner);
28    if csDesigning in ComponentState then
29      ReadComponentRes(Self.ClassName, Self);
30  end;

Now you have a nice subform control which can be used at run time or design time. 
You can do it with any form which you wish see as subform.

Note: You can define events handler for subform and them will work. In case subform 
already has some event handler defined and you try redefine it, only subform's 
event handler will work at run time!

Full source code of the subform control:

31  unit SubForm;
33  interface
35  uses
36    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Menus, Dialogs,
37      StdCtrls;
39  type
40    TSubForm = class(TForm)
41    protected
42      procedure CreateParams(var Params: TCreateParams); override;
43    public
44      constructor Create(aOwner: TComponent); override;
45      destructor Destroy; override;
46    end;
48  procedure register;
50  implementation
52  {$R *.DFM}
54  procedure register;
55  begin
56    RegisterComponents('SubForms', [TSubForm]);
57  end;
59  constructor TSubForm.Create(aOwner: TComponent);
60  begin
61    inherited Create(aOwner);
62    if (csDesigning in ComponentState) then
63      ReadComponentRes(Self.ClassName, Self);
64  end;
66  destructor TSubForm.Destroy;
67  begin
68    SetDesigning(False);
69    inherited Destroy;
70  end;
72  procedure TSubForm.CreateParams(var Params: TCreateParams);
73  begin
74    inherited CreateParams(Params);
75    Params.Style := WS_CHILD or WS_DLGFRAME or WS_VISIBLE or DS_CONTROL;
76  end;
78  end.

Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10


Share this page
Download from Google

Copyright © Mendozi Enterprises LLC