按钮打开表单
Button opens Form
我的问题是我想用过程 Button1Click(Sender: TObject); 创建新的表单和按钮。我遇到的问题是我不知道如何使用过程 TForm2.myClick(Sender: TObject); 打开新创建的表单。我希望通过不同的按钮创建不同的表单。
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs,Vcl.StdCtrls;
type
TForm2 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
procedure myClick(Sender: TObject);
end;
var
Form2: TForm2;
var
i:Integer = 1;
var
a:Integer = 1;
implementation
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
begin
with TButton.Create(self) do begin
Left := 80;
Top := 50 + i * 60;
Width := 300;
Height := 50;
Name := 'Liste' + IntToStr(i);
Self.Caption := ClassName+' '+ IntToStr(i);
OnClick := MyClick;
Parent :=Form2;
end;
i:= i + 1;
begin
with TForm.Create(Self) do begin
Left := 0;
Top := 0;
Width := 1000;
Height := 700;
Name := 'Erinnerung' + IntToStr(a);
Caption := 'Erinnerung' + IntToStr(a);
Parent := self;
OnClick:= MyClick;
Visible:=false;
end;
a:= a + 1;
end;
end;
procedure TForm2.myClick(Sender: TObject);
begin
end;
end.
您的问题中有一些未指定的项目。无论如何,我做了一个简单的例子来说明可以做什么。您将开始并修改此代码以满足您的需要。
此代码有一个带有单个按钮的表单 (TForm1)。每次用户单击该按钮时,都会创建一个新按钮和一个新表单。创建的按钮显示在 TForm1 上的一列中,就像您所做的那样。表单已创建但不像您所做的那样可见。列表保留对创建的按钮和表单的引用。
创建的按钮和表单的标签 属性 设置为列表中的索引。按钮的 OnClick 事件处理程序使用此索引从列表中检索相应的表单并使其可见。
表单是用 nil Parent 创建的,因此它是飞行的 window。如果您使用 Self 而不是 nil,则创建的表单将位于 TForm1 中,很像 MDI 应用程序。
代码如下:
unit DynFormDemoMain;
interface
uses
Winapi.Windows, Winapi.Messages,
System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
System.Generics.Collections;
type
TFormItem = record
AForm : TForm;
AButton : TButton;
end;
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
FList : TList<TFormItem>;
procedure MyClick(Sender : TObject);
public
constructor Create(AOwner : TComponent); override;
destructor Destroy; override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
Item : TFormItem;
AButton : TButton;
AForm : TForm;
begin
AButton := TButton.Create(Self);
AButton.Tag := FList.Count;
AButton.Left := 80;
AButton.Top := 50 + AButton.Tag * 60;
AButton.Width := 300;
AButton.Height := 50;
AButton.Name := 'Liste' + IntToStr(AButton.Tag);
AButton.Caption := ClassName + ' ' + IntToStr(AButton.Tag);
AButton.OnClick := MyClick;
AButton.Parent := Self;
AForm := TForm.Create(Self);
AForm.Tag := AButton.Tag;
AForm.Left := AButton.Tag * 20;
AForm.Top := AButton.Tag * 20;
AForm.Width := 200;
AForm.Height := 100;
AForm.Name := 'Erinnerung' + IntToStr(AButton.Tag);
AForm.Caption := 'Erinnerung' + IntToStr(AButton.Tag);
AForm.Parent := nil;
AForm.Visible := FALSE;
Item.AForm := AForm;
Item.AButton := AButton;
FList.Add(Item);
end;
constructor TForm1.Create(AOwner: TComponent);
begin
inherited;
FList := TList<TFormItem>.Create;
end;
destructor TForm1.Destroy;
begin
FreeAndNil(FList);
inherited;
end;
procedure TForm1.MyClick(Sender: TObject);
var
Item : TFormItem;
begin
Item := FList[(Sender as TButton).Tag];
if Assigned(Item.AForm) then
Item.AForm.Show;
end;
end.
我的问题是我想用过程 Button1Click(Sender: TObject); 创建新的表单和按钮。我遇到的问题是我不知道如何使用过程 TForm2.myClick(Sender: TObject); 打开新创建的表单。我希望通过不同的按钮创建不同的表单。
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs,Vcl.StdCtrls;
type
TForm2 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
procedure myClick(Sender: TObject);
end;
var
Form2: TForm2;
var
i:Integer = 1;
var
a:Integer = 1;
implementation
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
begin
with TButton.Create(self) do begin
Left := 80;
Top := 50 + i * 60;
Width := 300;
Height := 50;
Name := 'Liste' + IntToStr(i);
Self.Caption := ClassName+' '+ IntToStr(i);
OnClick := MyClick;
Parent :=Form2;
end;
i:= i + 1;
begin
with TForm.Create(Self) do begin
Left := 0;
Top := 0;
Width := 1000;
Height := 700;
Name := 'Erinnerung' + IntToStr(a);
Caption := 'Erinnerung' + IntToStr(a);
Parent := self;
OnClick:= MyClick;
Visible:=false;
end;
a:= a + 1;
end;
end;
procedure TForm2.myClick(Sender: TObject);
begin
end;
end.
您的问题中有一些未指定的项目。无论如何,我做了一个简单的例子来说明可以做什么。您将开始并修改此代码以满足您的需要。
此代码有一个带有单个按钮的表单 (TForm1)。每次用户单击该按钮时,都会创建一个新按钮和一个新表单。创建的按钮显示在 TForm1 上的一列中,就像您所做的那样。表单已创建但不像您所做的那样可见。列表保留对创建的按钮和表单的引用。
创建的按钮和表单的标签 属性 设置为列表中的索引。按钮的 OnClick 事件处理程序使用此索引从列表中检索相应的表单并使其可见。
表单是用 nil Parent 创建的,因此它是飞行的 window。如果您使用 Self 而不是 nil,则创建的表单将位于 TForm1 中,很像 MDI 应用程序。
代码如下:
unit DynFormDemoMain;
interface
uses
Winapi.Windows, Winapi.Messages,
System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
System.Generics.Collections;
type
TFormItem = record
AForm : TForm;
AButton : TButton;
end;
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
FList : TList<TFormItem>;
procedure MyClick(Sender : TObject);
public
constructor Create(AOwner : TComponent); override;
destructor Destroy; override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
Item : TFormItem;
AButton : TButton;
AForm : TForm;
begin
AButton := TButton.Create(Self);
AButton.Tag := FList.Count;
AButton.Left := 80;
AButton.Top := 50 + AButton.Tag * 60;
AButton.Width := 300;
AButton.Height := 50;
AButton.Name := 'Liste' + IntToStr(AButton.Tag);
AButton.Caption := ClassName + ' ' + IntToStr(AButton.Tag);
AButton.OnClick := MyClick;
AButton.Parent := Self;
AForm := TForm.Create(Self);
AForm.Tag := AButton.Tag;
AForm.Left := AButton.Tag * 20;
AForm.Top := AButton.Tag * 20;
AForm.Width := 200;
AForm.Height := 100;
AForm.Name := 'Erinnerung' + IntToStr(AButton.Tag);
AForm.Caption := 'Erinnerung' + IntToStr(AButton.Tag);
AForm.Parent := nil;
AForm.Visible := FALSE;
Item.AForm := AForm;
Item.AButton := AButton;
FList.Add(Item);
end;
constructor TForm1.Create(AOwner: TComponent);
begin
inherited;
FList := TList<TFormItem>.Create;
end;
destructor TForm1.Destroy;
begin
FreeAndNil(FList);
inherited;
end;
procedure TForm1.MyClick(Sender: TObject);
var
Item : TFormItem;
begin
Item := FList[(Sender as TButton).Tag];
if Assigned(Item.AForm) then
Item.AForm.Show;
end;
end.