?
Solved

Best access method for embedded components

Posted on 1998-10-06
6
Medium Priority
?
171 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month8 days, 23 hours left to enroll

764 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