Link to home
Start Free TrialLog in
Avatar of Edo082297
Edo082297

asked on

Best access method for embedded components

Hello All,
   I have a composite component contained within a TPanel. I want to know the best way to expose properties of the embedded controls to users of the component. Events are easy to expose because they are pointers, so I can hook them up in the constructor. However, I want to know if I can directly hook exposed properties to inherited properties of contained components, without going through a mission. Somebody has a neat way, I'm sure.
   Consider the simplistic example of the Color property. If a user changes the Color property, the (container) TPanel changes color. I would like it to change one or more of the contained component's color properties (and leave the TPanel's color alone). Since Color is inherited by both components, it would be nice to avoid having to create a placeholder. The complexity increases when considering that a user may change the color at runtime; I need to make a deep copy of the reference (as opposed to a kludge which may set the color once properly in the constructor). It seems silly to me to have to redeclare TColor and then hook into a color change event etc.... for each property that I wish to expose. This functionality is so easily accomplished with events; is there a way to do this for non-event type properties?

Edo
Avatar of Edo082297
Edo082297

ASKER

Edited text of question
I am not sure what your question is. Maybe a little bit of code that you tried to do something and a bit more explanation would help...Don't you think??

Regards,
Viktor Ivanov
You can use the Controls property of TPanel to iterate all embedded controls. Control stores TControls. They have a Color/Enabled/Font property which are update in a for-loop. For different properties you have to TypeCast the TControl. This is not neat.

To give more info I'd like to know. The embedded controls are always the same controls? Or can the amount/type of embedded controls be varied at designtime?

Regards Jacco
Ok. Edo.

You need stubs for your properties on the container control (in your case, the panel).
Try this:
>>>>>>>>>>>>>>boc>>>>>>>>

unit EdoPanel;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, StdCtrls;

type
  TEdoPanel = class(TCustomPanel)
  private
    { Private-Deklarationen }
    FLabel1: TLabel;
    FLabel2: TLabel;
    FButton: TButton;
    procedure SetColor(AValue: TColor);
    function GetColor: TColor;
  protected
    { Protected-Deklarationen }
    procedure CreateWindowHandle(const Params: TCreateParams); override;
  public
    { Public-Deklarationen }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    { Published-Deklarationen }
    property Color: TColor read GetColor write SetColor;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('ExEx', [TEdoPanel]);
end;

constructor TEdoPanel.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  BevelOuter := bvNone;
  BevelInner := bvNone;
  Width := 200;
  Height := 100;
  TabOrder := 0;

  FLabel1 := TLabel.Create(self);
  FLabel1.Parent := Self;

  FLabel2 := TLabel.Create(self);
  FLabel2.Parent := Self;

  FButton:= TButton.Create(self);
  FButton.Parent := Self;
end;

destructor TEdoPanel.Destroy;
begin
  inherited Destroy;
end;

{ call inherited CreateWindowHandle and initialize subcomponents. }
procedure TEdoPanel.CreateWindowHandle(const Params: TCreateParams);
begin
  inherited CreateWindowHandle(Params);

  with FLabel1 do begin
    Caption := 'Edo''s Label 1';
    Left := 10;
    Top := 10;
    Width := 100;
    Visible := true;
  end;

  with FLabel2 do begin
    Caption := 'Edo''s Label 2';
    Left := 10;
    Top := 30;
    Width := 100;
    Visible := true;
  end;

  with FButton do begin
    Caption := 'Click!';
    Left := 20;
    Top := 50;
    Width := 40;
    Visible := true;
  end;

end;

procedure TEdoPanel.SetColor(AValue: TColor);
begin
  FLabel1.Color := AValue;
  FLabel2.Color := AValue;
end;

function TEdoPanel.GetColor: TColor;
begin
  Result := FLabel1.Color;
end;

end.

<<<<<<<<<<<<<<<<<<<<eoc<<<<<<<<<<<<<<<<<<<


Hi Freter
   Actually, the solution that I came up with is exactly that. I provide get and set access methods which just talk to the subcomponent I wish to reference:

procedure TEdoPanel.SetColor(Value : TColor);
begin
  if FSubComponentIWishToExpose.Color <> Value then
  begin
    FSubComponentIWishToExpose.Color := Value;
    Invalidate;
  end;  
end;

This is the easiest and clearest way to implement this functionality. I just thought someone might know how to pass the address of a subcomponent property to the container property declaration -- which would avoid having to write set and get access methods for each property that I wish to expose (it would additionally be a deep copy ). However, since the storage of these is private to the container, there is no way to accomplish this.
  Post an answer, I'll give you the points as I am relieved to have reinforcement of my methodology.

Regards,
Edo

PS Freter, I got some units of yours at one point relating to network API (you had a number of units on the net?) but they didn't compile properly (I hope I have the right guy). Do you have an updated version somewhere? I liked the code and would like to see (steal?!) more of your work... If it works(!).
ASKER CERTIFIED SOLUTION
Avatar of freter
freter

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial