我正在构建一个非常简单的 Firemonkey 项目 (RadStudio 10.3.3),以测试未来项目的某些布局选项。在过去的 VCL 中,我使用了模态形式。我正在测试的项目在主窗体 (Form1) 上使用面板(Panel1 和 Panel2)来嵌入两个附加窗体(Form2 和 Form3)。这两个嵌入的表单由每个表单上的单个列表框 (ListBox1) 组成。主窗体上的面板覆盖,所以我使用 Visibility 属性来显示我想要的嵌入窗体。所有代码都在主窗体上。
我遇到的问题是,当我在 Form2 和 Form3 之间切换时,加载到 Form3 上的列表框中的字符串永远不会出现。我在列表框和面板上尝试过 Repaint,在列表框上尝试过 InvalidateRect,在面板上尝试过 SetFocus 等,后面都是 Application.ProcessMessages。没有什么能成功。
主要代码是:
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
FMX.Controls.Presentation, FMX.StdCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
Button1: TButton;
Panel2: TPanel;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
procedure EmbedForm(AParent:TControl; AForm:TCustomForm);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
uses Unit2, Unit3;
procedure TForm1.FormCreate(Sender: TObject);
begin
// Embed Form2 in Panel1
Application.CreateForm(TForm2, Form2);
EmbedForm(Panel1, Form2);
Panel1.Visible := true;
// Embed Form3 in Panel2
Application.CreateForm(TForm3, Form3);
EmbedForm(Panel2, Form3);
Panel2.Visible := false;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
// Populate ListBox1 on Form2 - the LOAD button
Form2.ListBox1.Items.Add('Hello');
Form2.ListBox1.Items.Add('World');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
// Hide Panel1 (Form2) and show Panel2 (Form3)
Panel1.Visible := false;
Panel2.Visible := true;
// Populate ListBox1 on Form3
Form3.ListBox1.Items.Add('Goodbye');
Form3.ListBox1.Items.Add('World');
// Repaint (Here's why I have tried various things to get the listbox strings to show up)
//Panel2.Repaint;
//Form3.ListBox1.Repaint;
//Application.ProcessMessages;
end;
procedure TForm1.EmbedForm(AParent: TControl; AForm: TCustomForm);
begin
while AForm.ChildrenCount>0 do
AForm.Children[0].Parent:=AParent;
end;
end.
表格2如下:
unit Unit2;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Layouts,
FMX.ListBox;
type
TForm2 = class(TForm)
ListBox1: TListBox;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.fmx}
end.
Form3如下:
unit Unit3;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Layouts,
FMX.ListBox, FMX.Controls.Presentation, FMX.StdCtrls;
type
TForm3 = class(TForm)
ListBox1: TListBox;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form3: TForm3;
implementation
{$R *.fmx}
end.
根据要求,.fmx 文件如下所示。
Unit1.fmx (Form1):
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 480
ClientWidth = 640
FormFactor.Width = 320
FormFactor.Height = 480
FormFactor.Devices = [Desktop]
OnCreate = FormCreate
DesignerMasterStyle = 0
object Button1: TButton
Position.X = 232.000000000000000000
Position.Y = 448.000000000000000000
TabOrder = 1
Text = 'Load'
OnClick = Button1Click
end
object Button2: TButton
Position.X = 328.000000000000000000
Position.Y = 448.000000000000000000
TabOrder = 2
Text = 'Next'
OnClick = Button2Click
end
object Panel1: TPanel
Align = Center
Size.Width = 640.000000000000000000
Size.Height = 393.000000000000000000
Size.PlatformDefault = False
TabOrder = 0
end
object Panel2: TPanel
Position.Y = 43.000000000000000000
Size.Width = 640.000000000000000000
Size.Height = 393.000000000000000000
Size.PlatformDefault = False
TabOrder = 4
end
end
Unit2.fmx (Form2):
object Form2: TForm2
Left = 0
Top = 0
Caption = 'Form2'
ClientHeight = 480
ClientWidth = 640
FormFactor.Width = 320
FormFactor.Height = 480
FormFactor.Devices = [Desktop]
DesignerMasterStyle = 0
object ListBox1: TListBox
Align = Center
TabOrder = 0
DisableFocusEffect = True
DefaultItemStyles.ItemStyle = ''
DefaultItemStyles.GroupHeaderStyle = ''
DefaultItemStyles.GroupFooterStyle = ''
Viewport.Width = 200.000000000000000000
Viewport.Height = 200.000000000000000000
end
end
Unit3.fmx (Form3):
object Form3: TForm3
Left = 0
Top = 0
Caption = 'Form3'
ClientHeight = 480
ClientWidth = 640
FormFactor.Width = 320
FormFactor.Height = 480
FormFactor.Devices = [Desktop]
DesignerMasterStyle = 0
object ListBox1: TListBox
Position.X = 8.000000000000000000
Position.Y = 8.000000000000000000
TabOrder = 1
DisableFocusEffect = True
DefaultItemStyles.ItemStyle = ''
DefaultItemStyles.GroupHeaderStyle = ''
DefaultItemStyles.GroupFooterStyle = ''
Viewport.Width = 196.000000000000000000
Viewport.Height = 196.000000000000000000
end
end
同样,Form2 和 Form3 都只包含一个列表框(两者都包含 Listbox1)并且没有其他代码。我只需运行可执行文件,单击 Button1 显示 Hello World,然后单击 Button2 切换面板并显示第二个表单及其列表框。由于我是 Firemonkey 的新手,我确信我缺少一些简单的东西。感谢您的任何帮助!
解决方案非常简单。我不得不从项目的初始化设置中删除 Form2 和 Form3 的 CreateForm 事件——这对我来说是一个愚蠢的错误。它在执行过程中失去了对这些表格的引用。