maxfax
maxfax

Reputation: 4305

Delphi: communication among Frames

How to communicate among frames and within a frame? For example: a Frame 1 and a Frame 2.

The frame 2 is in the frame 1. To insert the frame 2 into the frame 1 I add frames from 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

But I have an error trying to access to MyFrame1 and to do something like MyFrame1.Button1.Enable within the frame 1 or from other frames: "Exception class EAccessViolation with a message 'Access violation at address 0084858C in module 'P1.exe'"

How to access to the frame 1 from the frame 2? MyFrame1->Error.

Thanks!

Upvotes: 2

Views: 627

Answers (2)

Rudy Velthuis
Rudy Velthuis

Reputation: 28806

TOndrej mentioned using the Owner, but that is usually the form, not Frame1. The Parent of Frame2 should be Frame1, so:

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; 

Updated added code to use Parent.Controls to find a TFrame1.

Upvotes: 0

Ondrej Kelle
Ondrej Kelle

Reputation: 37211

Please delete the global variable declaration:

var MyFrame1: TFrame1;

It usually makes no sense for frames.

You can typecast the child frame's Owner to TFrame1, for example:

implementation

uses
  FrameUnit1;

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

Upvotes: 2

Related Questions