问题描述
我可以使用此处给出的解决方案在运行时创建TABSHEETS,.在我的用例中,我需要创建动态数量的表单,在该页面控件上创建3个不同的子表单.
I'm able to create TABSHEETS at run time using the solution given here TAB AT RUN TIME .In my use case I need to create an dynamic number of forms, I create 3 different subforms on that page control.
如何创建动态表单集以及如何在运行时处理这些控件?
How to Create a dynamic set of forms and how to address these controls at run time ?
MyForm1 := CreateTabAndForm_type_1;
MyForm2 := CreateTabAndForm_type_1;
MyForm3 := CreateTabAndForm_type_1;
....
???
推荐答案
下面是一个小示例,介绍了如何向PageControl添加表单以及如何从TabSheet中获取表单实例.
Here is a small sample how to add a form to a PageControl and how to get the form instance back from the TabSheet.
- 窗体通过
TControl.ManualDock
停靠到PageControl. - 因此可以通过
TTabSheet.Controls [0]
(该表单是此TabSheet上的第一个控件)访问停靠的表单
- forms are docked to the PageControl with
TControl.ManualDock
- so docked forms are accessible through
TTabSheet.Controls[0]
(the form is the first control on this TabSheet)
该示例只是一个POC,并未涵盖您在实际应用程序中将要进行的所有检查.
type
TMainForm = class( TForm )
PageControl1 : TPageControl;
Button_Panel : TPanel;
AddForm_Button : TButton;
PressButtonOnSubForm_Button: TButton;
procedure AddForm_ButtonClick( Sender : TObject );
procedure PressButtonOnSubForm_ButtonClick(Sender: TObject);
private
function CreateFormInPageControl( AFormClass : TFormClass; APageControl : TPageControl ) : TForm;
function GetFormFromTabSheet( ATabSheet : TTabSheet ) : TForm;
function GetActivePageControlForm( APageControl : TPageControl ) : TForm;
public
{ Public-Deklarationen }
end;
implementation
uses
FormSub;
procedure TMainForm.PressButtonOnSubForm_ButtonClick(Sender: TObject);
begin
// push the button
if Assigned( PageControl1.ActivePage ) then
( GetActivePageControlForm(PageControl1) as TSubForm ).Button1.Click;
end;
procedure TMainForm.AddForm_ButtonClick( Sender : TObject );
var
LForm : TForm;
begin
LForm := CreateFormInPageControl( TSubForm, PageControl1 );
end;
function TMainForm.CreateFormInPageControl( AFormClass : TFormClass; APageControl : TPageControl ) : TForm;
begin
// create a new form from the given form class
Result := AFormClass.Create( Self );
// dock the form to the give page control
Result.ManualDock( APageControl, nil, alClient );
// show the form
Result.Show;
end;
function TMainForm.GetActivePageControlForm( APageControl : TPageControl ) : TForm;
begin
Result := GetFormFromTabSheet( APageControl.ActivePage );
end;
function TMainForm.GetFormFromTabSheet( ATabSheet : TTabSheet ) : TForm;
begin
Result := ATabSheet.Controls[0] as TForm;
end;
子窗体
type
TSubForm = class( TForm )
Button1 : TButton;
ListBox1 : TListBox;
procedure Button1Click( Sender : TObject );
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
procedure TSubForm.Button1Click( Sender : TObject );
begin
// just to put some action to this form
ListBox1.ItemIndex := ListBox1.Items.Add( 'Button pressed' );
end;
更新
如果您想摆脱停靠的表格,只需致电(针对当前活动表格)
Update
If you want to get rid of a docked form you simply call (for the current active form)
GetActivePageControlForm( PageControl1 ).Release;
TabSheet也将消失.
The TabSheet will disappear then too.
这篇关于管理表单的动态数组(在页面控件内部)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!