Delphi:Frames之间的通信

时间:2011-07-18 09:51:52

标签: delphi frames

如何在帧之间和帧内进行通信? 例如:第1帧和第2帧。

框架2在框架1中。为了将框架2插入框架1中,我从ToolPalette添加框架 - >

type
  TFrame1 = class(TFrame)
  Frame22: TFrame2;

  var MyFrame1:TFrame1; // Now I can access to everything within a frame and from other frames too
implementation

但是我尝试访问MyFrame1并在第1帧或其他帧中执行类似MyFrame1.Button1.Enable的操作时出错: “异常类EAccessViolation,消息'模块'P1.exe'中地址0084858C的访问冲突'”

如何从第2帧访问第1帧? MyFrame1->错误

谢谢!

2 个答案:

答案 0 :(得分:2)

请删除全局变量声明:

var MyFrame1: TFrame1;

帧通常没有意义。

您可以将子框架的Owner类型转换为TFrame1,例如:

implementation

uses
  FrameUnit1;

procedure TFrame2.Test;
begin
  if Owner is TFrame1 then
    ShowMessage(TFrame1(Owner).Name);
end;

答案 1 :(得分:0)

TOndrej提到使用所有者,但这通常是表单,而不是Frame1。 Frame2的Parent应该是Frame1,所以:

uses
  Frame1Unit;

procedure TFrame2.Test;
var
  C: TControl;
begin
  if Parent is TFrame1 then
    ShowMessage(TFrame1(Parent).Name)
  else 
    for C in Parent.Controls do
      if C is TFrame1 then
        ShowMessage(TFrame1(C).Name);
end; 

更新了添加的代码,以使用Parent.Controls查找TFrame1。