Solved

Best access method for embedded components

Posted on 1998-10-06
6
163 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
Comment Utility
Edited text of question
0
 
LVL 10

Expert Comment

by:viktornet
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 2

Expert Comment

by:freter
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now