我应该将delphi tframe用于多页表单吗?

Lep*_*eus 9 delphi tframe

我的应用程序中有一些表单具有不同的"状态",具体取决于用户的操作; 例如,当通过他的文件列出时,表单会在网格中显示有关该文件的一些数据,但如果他点击某个按钮,则网格会被与其相关的图表替换.简单地说,表单中的控件取决于用户想要做什么.

当然,这样做的显而易见的方法是根据需要显示/隐藏控件,就像小数字的魅力一样,但是一旦你达到每个状态10/15 +控件(或者真的超过3个状态)它就无法使用.

我现在正在尝试使用TFrame:我为每个状态创建一个框架,然后在我的表单上创建每个框架的实例,然后我只使用Visible显示我想要的那个 - 同时对其进行一些控制最重要的是,因为他们都共享它们.

这是做我想要的正确方法,还是我错过了一些东西?我以为我只能创建一个tframe实例,然后选择在其中显示哪个实例,但它看起来并不那样.

谢谢

Hen*_*man 17

看起来Frames是这种情况的绝佳选择.我想补充一点,您可以使用Base Frame和Visual Inheritance来创建一个通用接口.

第二部分:你设计一个像一个表格的框架,但你像控制一样使用它,很少有限制.请注意,您可以轻松使用"创建/自由"而不是"显示/隐藏".什么是更好的取决于他们是如何资源丰富.


Tim*_*van 10

有一种更好的方法来处理不会占用几乎相同内存的帧.动态创建框架可以是一个非常优雅的解决方案.这就是我过去的表现.

在表单上,​​添加一个属性和一个处理它在表单上的位置的setter:

TMyForm = class(TForm)
private
  FCurrentFrame : TFrame;
  procedure SetCurrentFrame(Value : TFrame);
public
  property CurrentFrame : TFrame read FCurrentFrame write SetCurrentFrame;
end;

procedure TMyForm.SetCurrentFrame(Value : TFrame)
begin
  if Value <> FCurrentFrame then
  begin
    if assigned(FCurrentFrame) then
      FreeAndNil(FCurrentFrame);
    FCurrentFrame := Value;
    if assigned(FCurrentFrame) then
    begin
      FCurrentFrame.Parent := Self;  // Or, say a TPanel or other container!
      FCurrentFrame.Align := alClient;
    end;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

然后,要使用它,只需将属性设置为框架的已创建实例,例如在OnCreate事件中:

MyFrame1.CurrentFrame := TSomeFrame.Create(nil);
Run Code Online (Sandbox Code Playgroud)

如果你想摆脱框架,只需分配nil给CurrentFrame属性:

MYFrame1.CurrentFrame := nil;
Run Code Online (Sandbox Code Playgroud)

它非常好用.