Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Best access method for embedded components

Posted on 1998-10-06
6
Medium Priority
?
174 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 400 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month21 days, 4 hours left to enroll

810 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