gpt4 book ai didi

delphi - 嵌入式表单控件不刷新(Delphi、Firemonkey)

转载 作者:行者123 更新时间:2023-12-03 18:12:39 25 4
gpt4 key购买 nike

我正在构建一个非常简单的 Firemonkey 项目 (RadStudio 10.3.3),以测试 future 项目的某些布局选项。在过去的 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 事件——这对我来说是一个愚蠢的错误。它在执行过程中失去了对这些表格的引用。

最佳答案

我可以重现错误的唯一方法是将表单和两个面板之间的父子关系设置错误。

例如。如果Panel2,我可以重现您描述的错误行为。是 Panel1 的 child ,但如果他们都是表单的 child ,我无法重现该问题。也许您将第二个面板放在 Panel1 上。因为失误?检查 IDE 中的结构窗口。

编辑 .fmx 之后文件已添加到问题中

查看提供的 Unit1.fmx像这样结束:

  object Panel1: TPanel
Align = Center
Size.Width = 640.000000000000000000
Size.Height = 393.000000000000000000
Size.PlatformDefault = False
TabOrder = 0
object Layout1: TLayout
Align = Contents
Size.Width = 640.000000000000000000
Size.Height = 393.000000000000000000
Size.PlatformDefault = False
TabOrder = 0
end
end

没有 Panel2根据 Unit1.pas 应该有.取而代之的是 Layout1小时候到 Panel1 .所以,现在发布的 .fmx文件与 .pas 不匹配你昨天提供的文件。但它可能仍然证实了我已经说过的话。

如果 Panel2已被此布局取代,换句话说, Panel2Panel1的 child ,那么这将完全解释您最初询问的行为。

关于delphi - 嵌入式表单控件不刷新(Delphi、Firemonkey),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59254683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com