Solved

Best access method for embedded components

Posted on 1998-10-06
6
165 Views
Last Modified: 2013-11-23
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
0
Comment
Question by:Edo082297
6 Comments
 
LVL 1

Author Comment

by:Edo082297
ID: 1342029
Edited text of question
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1342030
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
0
 
LVL 10

Expert Comment

by:Jacco
ID: 1342031
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
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 2

Expert Comment

by:freter
ID: 1342032
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<<<<<<<<<<<<<<<<<<<


0
 
LVL 1

Author Comment

by:Edo082297
ID: 1342033
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(!).
0
 
LVL 2

Accepted Solution

by:
freter earned 100 total points
ID: 1342034
Heidiho, Edo.
Here's the answer.

Regarding the network API units: you may well have some units concerning the explorer from me, but i don't have any hands in networking apis (though i'd like to). Sorry ;-)

Freter
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question