我是 Firemonkey 的新手(使用 RadStudio 10.3.2),我正在尝试更新嵌入式子窗体上的列表框控件。但是,当我尝试访问列表框 (ListBox1) 的任何属性时,它们显示为“无法访问的值”。我很确定我错过了一些非常简单的东西。我感谢任何和所有的帮助!谢谢!
我做了以下简化的应用程序来说明我的问题。
Project1 应用程序初始化:
program Project1;
uses
System.StartUpCopy,
FMX.Forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas' {Form2};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
单元1.pas:
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;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure EmbedForm(AParent:TControl; AForm:TCustomForm);
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
uses Unit2;
procedure TForm1.FormCreate(Sender: TObject);
begin
// Embed Scenarios Form
EmbedForm(Panel1, TForm2.Create(Self));
Panel1.Visible := true;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
data_strings: array of string;
begin
// Assign strings
SetLength(data_strings, 2);
data_strings[0] := 'Hello';
data_strings[1] := 'World';
// Load strings
Form2.ListBox1.Items.Add(data_strings[0]);
Form2.ListBox1.Items.Add(data_strings[1]);
end;
procedure TForm1.EmbedForm(AParent: TControl; AForm: TCustomForm);
begin
while AForm.ChildrenCount>0 do
AForm.Children[0].Parent:=AParent;
end;
end.
单元2.pas:
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.
主窗体上的代码无法访问 Form2.ListBox1 属性 - 请参阅 Button1Click 事件。我有使用子表单(Unit2)的父表单(Unit1)。
我不知道为什么无法访问已启用的可见组件。我的模态表单从来没有这个问题,所以我认为它与嵌入的子表单有关。