创建新表单
Creation of a new Form
我更新了我的代码并查看了它。问题是当我尝试在 AForm 中使用过程 TForm2.Next1Click(Sender:TObject) 生成 NextButton4 和 NextButton3(这是由 TForm2.Button1Click 创建的我的表单)时,它不会像我在中看不到 NextButton4 和 NextButton3 一样工作表单。 Delphi 告诉我,AForm 可能无法在第 117 行初始化(表示 NextButton3 := TButton.Create(AForm);),但我不知道如何修复它。对不起,我没有很好地解释它。 (TForm2.Next1Click(Sender:TObject); 的可点击组件是 NextButton1)。
我的目标是创建一个有效的 ToDoList。为此,我需要一个 Button1 所在的 Mainform。单击 Button1 现在会在 Mainform(Form2) 上创建 lists/folders (AButton),并在现在位于 NextButton1 和 NextButton2 的位置创建一个新表单 (AForm)。现在我想在 AForm 中存储记忆、任务等,这就是为什么我需要另外两个按钮(NextButton3 和 NextButton4)的原因。 NextButton2 应该只是关闭窗体 (AForm)。如果单击 NextButton1,则应创建 2 个按钮(NextButton3 和 NextButton4)和另一个表单 (NextForm)。最后我想在 AForm 中添加一个面板来存储记忆和任务(还没有添加面板)。 NextButton4 应该打开 NextForm。在 NextForm 中,您可以编辑我尚未创建的面板(f.e。更改 memory/task 的颜色或名称)。这个问题是创建了 NextButton3 和 NextButton4(我认为)但我看不到它。希望你能看懂我写的。
unit Unit2;
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;
NextForm : TForm;
AButton : TButton;
NextButton1 : TButton;
NextButton2 : TButton;
NextButton3 : TButton;
NextButton4 : TButton;
end;
TForm2 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
FList1 : TList<TFormItem>;
FList2 : TList<TFormItem>;
procedure MyClick (Sender:TObject);
procedure Next1Click(Sender:TObject);
procedure Next2Click(Sender:TObject);
procedure Next3Click(Sender:TObject);
procedure Next4Click(Sender:TObject);
public
{ Public-Deklarationen }
constructor Create(AOwner : TComponent); override;
destructor Destroy; override;
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
var
Item : TFormItem;
AButton : TButton;
AForm : TForm;
NextForm : TForm;
NextButton1 : TButton;
NextButton2 : TButton;
NextButton4 : TButton;
begin
AButton := TButton.Create(Self);
AButton.Tag := FList1.Count;
AButton.Left := 80;
AButton.Top := 50 + AButton.Tag * 60;
AButton.Width := 300;
AButton.Height := 50;
AButton.Name := 'Liste' + IntToStr(1+AButton.Tag);
AButton.Caption := 'Liste' + IntToStr(1+AButton.Tag);
AButton.OnClick := MyClick;
AButton.Parent := Self;
AForm := TForm.Create(Self);
AForm.Tag := AButton.Tag;
AForm.Left := AButton.Tag;
AForm.Top := AButton.Tag;
AForm.Width := 1000;
AForm.Height := 700;
AForm.Name := 'Erinnerung' + IntToStr(1+AButton.Tag);
AForm.Caption := 'Erinnerung' + IntToStr(1+AButton.Tag);
AForm.Parent := nil;
AForm.Visible := FALSE;
NextButton1 := TButton.Create(AForm);
NextButton1.Tag := FList1.Count;
NextButton1.Left := 20;
NextButton1.Top := 560 ;
NextButton1.Width := 170;
NextButton1.Height := 70;
NextButton1.Name := 'Erinnerung_hinzufügen';
NextButton1.Caption := 'Erinnerung_hinzufügen';
NextButton1.OnClick := Next1Click;
NextButton1.Parent := AForm;
NextButton1.Visible := true;
NextButton2 := TButton.Create(AForm);
NextButton2.Tag := FList1.Count;
NextButton2.Left := 800;
NextButton2.Top := 560;
NextButton2.Width := 170;
NextButton2.Height := 70;
NextButton2.Name := 'Ok';
NextButton2.Caption := 'Ok';
NextButton2.OnClick := Next2Click;
NextButton2.Parent := AForm;
NextButton2.Visible := true;
Item.AForm := AForm;
Item.AButton := AButton;
FList1.Add(Item);
end;
procedure TForm2.Next1Click(Sender:TObject);
var
NextForm : TForm;
AButton : TButton;
Item : TFormItem;
AForm : TForm;
NextButton3 : TButton;
NextButton4 : TButton;
begin
NextButton3 := TButton.Create(AForm);
NextButton3.Tag := FList2.Count;
NextButton3.Left := 240;
NextButton3.Top := 2 + NextButton3.Tag * 60;
NextButton3.Width := 75;
NextButton3.Height := 25;
NextButton3.Name := 'Einstellungen' + IntToStr(1+NextButton3.Tag);
NextButton3.Caption := 'Einstellungen' + IntToStr(1+NextButton3.Tag);
NextButton3.OnClick := Next3Click;
NextButton3.Parent := AForm;
NextButton3.Visible := True;
NextButton4 := TButton.Create(Self);
NextButton4.Tag := FList2.Count;
NextButton4.Left := 120;
NextButton4.Top := 2 + NextButton4.Tag * 60;
NextButton4.Width := 75;
NextButton4.Height := 25;
NextButton4.Name := 'Einstellungen' + IntToStr(1+NextButton4.Tag);
NextButton4.Caption := 'Einstellungen' + IntToStr(1+NextButton4.Tag);
NextButton4.OnClick := Next4Click;
NextButton4.Parent := AForm;
NextButton4.Visible := True;
NextForm := TForm.Create(Self);
NextForm.Tag := NextButton4.Tag;
NextForm.Left := NextButton4.Tag;
NextForm.Top := NextButton4.Tag;
NextForm.Width := 1000;
NextForm.Height := 700;
NextForm.Name := 'Erinnerung' + IntToStr(1+NextButton4.Tag);
NextForm.Caption := 'Erinnerung' + IntToStr(1+NextButton4.Tag);
NextForm.Parent := nil;
NextForm.Visible := false;
Item.NextForm := NextForm;
Item.NextButton4 := NextButton4;
FList2.Add(Item);
end;
constructor TForm2.Create(AOwner: TComponent);
begin
inherited;
FList1 := TList<TFormItem>.Create;
FList2 := TList<TFormItem>.Create;
end;
destructor TForm2.Destroy;
begin
FreeAndNil(FList1);
FreeAndNil(FList2);
inherited;
end;
procedure TForm2.MyClick(Sender: TObject);
var
Item : TFormItem;
begin
Item := FList1[(Sender as TButton).Tag];
if Assigned(Item.AForm) then
Item.AForm.Show;
end;
procedure TForm2.Next2Click(Sender:TObject);
var
Item : TFormItem;
begin
Item := FList1[(Sender as TButton).Tag];
if Assigned(Item.AForm) then
Item.AForm.Close;
end;
procedure TForm2.Next3Click(Sender:TObject);
begin
end;
procedure TForm2.Next4Click(Sender:TObject);
var
Item : TFormItem;
begin
Item := FList2[(Sender as TButton).Tag];
if Assigned(Item.NextForm) then
Item.NextForm.Show;
end;
end.
在 TForm2.Next1Click
的末尾,您需要添加:
AForm.Show;
另外,在 TForm2.Next1Click
中你得到的警告是正确的:AForm
这里有一个 different 变量需要从某个地方获取它的值,并且这里根本没有得到值,所以可能包含 nil
或更糟的是,当调用此事件处理程序时,将使用由恰好存储在内存中的任何内容组成的值 Delphi .
如果您需要它与您在 TForm2.Next1Click
中创建的表单的引用具有相同的值,则必须将该值存储在某处。例如在 implementation
上面的全局 var
子句中,或者 TForm2
.
中的私有成员
例如,如果您在 var Form2:TForm2;
和 implementation
之间添加 Form3:TForm;
,则添加
Form3:=AForm;
在TForm2.Next1Click
和
的末尾
AForm:=Form3;
TForm2.Next1Click
开头
Delphi 说“AForm 可能尚未初始化”是完全正确的。毕竟,AForm
是 procedure TForm2.Next1Click()
的局部变量,您在使用它之前不会为其分配任何内容,在这种情况下作为所有者:
NextButton3 := TButton.Create(AForm);
稍后你写:
NextButton4 := TButton.Create(Self); // self is your Form2!!
您还需要 AForm
作为有效对象,以指定为您稍后在代码中创建的按钮的父对象
NextButton3.Parent := AForm;
NextButton4.Parent := AForm;
如果我没理解错的话,可能会创建一系列新表单,并且 NextButton3
和 NextButton4
的父级因情况而异。然而,我们可以使用 Sender: TObject
来识别被单击的 NextButton
。当我们知道按钮时,我们要求其父级知道表单。
因此,将其添加到 TForm2 的开头。Next1单击以设置 AForm
:
if TButton(Sender).Parent is TForm then
AForm := TForm(TButton(Sender).Parent);
然后您还需要修改 Name
(以避免名称冲突)和 Caption
赋值,例如:
// NextForm.Name := 'Erinnerung' + IntToStr(1+NextButton4.Tag);
NextForm.Name := AForm.Name + '_' + IntToStr(1+NextButton4.Tag);
// NextForm.Caption := 'Erinnerung' + IntToStr(1+NextButton4.Tag);
NextForm.Caption := AForm.Name + '_' + IntToStr(1+NextButton4.Tag);
我更新了我的代码并查看了它。问题是当我尝试在 AForm 中使用过程 TForm2.Next1Click(Sender:TObject) 生成 NextButton4 和 NextButton3(这是由 TForm2.Button1Click 创建的我的表单)时,它不会像我在中看不到 NextButton4 和 NextButton3 一样工作表单。 Delphi 告诉我,AForm 可能无法在第 117 行初始化(表示 NextButton3 := TButton.Create(AForm);),但我不知道如何修复它。对不起,我没有很好地解释它。 (TForm2.Next1Click(Sender:TObject); 的可点击组件是 NextButton1)。 我的目标是创建一个有效的 ToDoList。为此,我需要一个 Button1 所在的 Mainform。单击 Button1 现在会在 Mainform(Form2) 上创建 lists/folders (AButton),并在现在位于 NextButton1 和 NextButton2 的位置创建一个新表单 (AForm)。现在我想在 AForm 中存储记忆、任务等,这就是为什么我需要另外两个按钮(NextButton3 和 NextButton4)的原因。 NextButton2 应该只是关闭窗体 (AForm)。如果单击 NextButton1,则应创建 2 个按钮(NextButton3 和 NextButton4)和另一个表单 (NextForm)。最后我想在 AForm 中添加一个面板来存储记忆和任务(还没有添加面板)。 NextButton4 应该打开 NextForm。在 NextForm 中,您可以编辑我尚未创建的面板(f.e。更改 memory/task 的颜色或名称)。这个问题是创建了 NextButton3 和 NextButton4(我认为)但我看不到它。希望你能看懂我写的。
unit Unit2;
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;
NextForm : TForm;
AButton : TButton;
NextButton1 : TButton;
NextButton2 : TButton;
NextButton3 : TButton;
NextButton4 : TButton;
end;
TForm2 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
FList1 : TList<TFormItem>;
FList2 : TList<TFormItem>;
procedure MyClick (Sender:TObject);
procedure Next1Click(Sender:TObject);
procedure Next2Click(Sender:TObject);
procedure Next3Click(Sender:TObject);
procedure Next4Click(Sender:TObject);
public
{ Public-Deklarationen }
constructor Create(AOwner : TComponent); override;
destructor Destroy; override;
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
var
Item : TFormItem;
AButton : TButton;
AForm : TForm;
NextForm : TForm;
NextButton1 : TButton;
NextButton2 : TButton;
NextButton4 : TButton;
begin
AButton := TButton.Create(Self);
AButton.Tag := FList1.Count;
AButton.Left := 80;
AButton.Top := 50 + AButton.Tag * 60;
AButton.Width := 300;
AButton.Height := 50;
AButton.Name := 'Liste' + IntToStr(1+AButton.Tag);
AButton.Caption := 'Liste' + IntToStr(1+AButton.Tag);
AButton.OnClick := MyClick;
AButton.Parent := Self;
AForm := TForm.Create(Self);
AForm.Tag := AButton.Tag;
AForm.Left := AButton.Tag;
AForm.Top := AButton.Tag;
AForm.Width := 1000;
AForm.Height := 700;
AForm.Name := 'Erinnerung' + IntToStr(1+AButton.Tag);
AForm.Caption := 'Erinnerung' + IntToStr(1+AButton.Tag);
AForm.Parent := nil;
AForm.Visible := FALSE;
NextButton1 := TButton.Create(AForm);
NextButton1.Tag := FList1.Count;
NextButton1.Left := 20;
NextButton1.Top := 560 ;
NextButton1.Width := 170;
NextButton1.Height := 70;
NextButton1.Name := 'Erinnerung_hinzufügen';
NextButton1.Caption := 'Erinnerung_hinzufügen';
NextButton1.OnClick := Next1Click;
NextButton1.Parent := AForm;
NextButton1.Visible := true;
NextButton2 := TButton.Create(AForm);
NextButton2.Tag := FList1.Count;
NextButton2.Left := 800;
NextButton2.Top := 560;
NextButton2.Width := 170;
NextButton2.Height := 70;
NextButton2.Name := 'Ok';
NextButton2.Caption := 'Ok';
NextButton2.OnClick := Next2Click;
NextButton2.Parent := AForm;
NextButton2.Visible := true;
Item.AForm := AForm;
Item.AButton := AButton;
FList1.Add(Item);
end;
procedure TForm2.Next1Click(Sender:TObject);
var
NextForm : TForm;
AButton : TButton;
Item : TFormItem;
AForm : TForm;
NextButton3 : TButton;
NextButton4 : TButton;
begin
NextButton3 := TButton.Create(AForm);
NextButton3.Tag := FList2.Count;
NextButton3.Left := 240;
NextButton3.Top := 2 + NextButton3.Tag * 60;
NextButton3.Width := 75;
NextButton3.Height := 25;
NextButton3.Name := 'Einstellungen' + IntToStr(1+NextButton3.Tag);
NextButton3.Caption := 'Einstellungen' + IntToStr(1+NextButton3.Tag);
NextButton3.OnClick := Next3Click;
NextButton3.Parent := AForm;
NextButton3.Visible := True;
NextButton4 := TButton.Create(Self);
NextButton4.Tag := FList2.Count;
NextButton4.Left := 120;
NextButton4.Top := 2 + NextButton4.Tag * 60;
NextButton4.Width := 75;
NextButton4.Height := 25;
NextButton4.Name := 'Einstellungen' + IntToStr(1+NextButton4.Tag);
NextButton4.Caption := 'Einstellungen' + IntToStr(1+NextButton4.Tag);
NextButton4.OnClick := Next4Click;
NextButton4.Parent := AForm;
NextButton4.Visible := True;
NextForm := TForm.Create(Self);
NextForm.Tag := NextButton4.Tag;
NextForm.Left := NextButton4.Tag;
NextForm.Top := NextButton4.Tag;
NextForm.Width := 1000;
NextForm.Height := 700;
NextForm.Name := 'Erinnerung' + IntToStr(1+NextButton4.Tag);
NextForm.Caption := 'Erinnerung' + IntToStr(1+NextButton4.Tag);
NextForm.Parent := nil;
NextForm.Visible := false;
Item.NextForm := NextForm;
Item.NextButton4 := NextButton4;
FList2.Add(Item);
end;
constructor TForm2.Create(AOwner: TComponent);
begin
inherited;
FList1 := TList<TFormItem>.Create;
FList2 := TList<TFormItem>.Create;
end;
destructor TForm2.Destroy;
begin
FreeAndNil(FList1);
FreeAndNil(FList2);
inherited;
end;
procedure TForm2.MyClick(Sender: TObject);
var
Item : TFormItem;
begin
Item := FList1[(Sender as TButton).Tag];
if Assigned(Item.AForm) then
Item.AForm.Show;
end;
procedure TForm2.Next2Click(Sender:TObject);
var
Item : TFormItem;
begin
Item := FList1[(Sender as TButton).Tag];
if Assigned(Item.AForm) then
Item.AForm.Close;
end;
procedure TForm2.Next3Click(Sender:TObject);
begin
end;
procedure TForm2.Next4Click(Sender:TObject);
var
Item : TFormItem;
begin
Item := FList2[(Sender as TButton).Tag];
if Assigned(Item.NextForm) then
Item.NextForm.Show;
end;
end.
在 TForm2.Next1Click
的末尾,您需要添加:
AForm.Show;
另外,在 TForm2.Next1Click
中你得到的警告是正确的:AForm
这里有一个 different 变量需要从某个地方获取它的值,并且这里根本没有得到值,所以可能包含 nil
或更糟的是,当调用此事件处理程序时,将使用由恰好存储在内存中的任何内容组成的值 Delphi .
如果您需要它与您在 TForm2.Next1Click
中创建的表单的引用具有相同的值,则必须将该值存储在某处。例如在 implementation
上面的全局 var
子句中,或者 TForm2
.
例如,如果您在 var Form2:TForm2;
和 implementation
之间添加 Form3:TForm;
,则添加
Form3:=AForm;
在TForm2.Next1Click
和
AForm:=Form3;
TForm2.Next1Click
开头
Delphi 说“AForm 可能尚未初始化”是完全正确的。毕竟,AForm
是 procedure TForm2.Next1Click()
的局部变量,您在使用它之前不会为其分配任何内容,在这种情况下作为所有者:
NextButton3 := TButton.Create(AForm);
稍后你写:
NextButton4 := TButton.Create(Self); // self is your Form2!!
您还需要 AForm
作为有效对象,以指定为您稍后在代码中创建的按钮的父对象
NextButton3.Parent := AForm;
NextButton4.Parent := AForm;
如果我没理解错的话,可能会创建一系列新表单,并且 NextButton3
和 NextButton4
的父级因情况而异。然而,我们可以使用 Sender: TObject
来识别被单击的 NextButton
。当我们知道按钮时,我们要求其父级知道表单。
因此,将其添加到 TForm2 的开头。Next1单击以设置 AForm
:
if TButton(Sender).Parent is TForm then
AForm := TForm(TButton(Sender).Parent);
然后您还需要修改 Name
(以避免名称冲突)和 Caption
赋值,例如:
// NextForm.Name := 'Erinnerung' + IntToStr(1+NextButton4.Tag);
NextForm.Name := AForm.Name + '_' + IntToStr(1+NextButton4.Tag);
// NextForm.Caption := 'Erinnerung' + IntToStr(1+NextButton4.Tag);
NextForm.Caption := AForm.Name + '_' + IntToStr(1+NextButton4.Tag);