Newbie Question: Create a component using other components

Posted on 2004-11-11
Last Modified: 2010-04-05
I have spent countless hours trying to learn how to create a new component, and am coming up short.  I'm sure this question is easy for the experts, but I'm getting frustrated with the lack of answers out there.

I have found several tutorials on creating new components. Many of them mention my problem but never get into details.

I'm creating a component that is a TPanel.  Within that Panel there is a edit box and a label.  I would like to present to the object inspector only certain properties of the panel, and selected properties of the edit and the label.

I have figured out that if you want to "hide" some of the parameters, then you have to use a "TCustomPanel" and only publish the properties of the panel that I want (however, some of them like top/left/help still remain!?!?).  However, I can't for the life of me figure out how to publish the properties of the edit box or the label as properties of the new component.

Ideally, what I would like to do is call the component "MyComponent".  Within the object inspector, I would like to see:

"Heading" (string referring to "label.caption")
"Data" (string referring to "editbox.text")

I figured out that when I publish the properties, I can pass the entire object as a property, and I can see it as a sub item in object inspector.  When I change the value of it in the object inspector, I can see the values updated in the form, HOWEVER when I run the program, the values are reset to the defaults (only on the sub components!)


  TDataField = class (TCustomPanel)
    fHeading: TLabel;
    fData: TEditBox;

    fDataType: String;
    fDigits: Integer;
    fDecimals: Integer;
    fExpression: String;

    constructor Create(AOwner:TComponent); override;
    property Caption;
    property Heading: TLabel read fLabel write fLabel;
    property Data: TEditBox read fData write fData;
    property DataType: String read fDataType write fDataType;
    property Digits: Integer read fDigits write fDigits default 0;
    property Decimals: Integer read fDecimals write fDecimals default 2;
    property Expression: String read fExpression write fExpression;

I create my Label/Editbox in the constructor and give them the default values, but it doesn't appear at runtime the program is passing in the values from the object inspector....but this is only the case with the label/editbox.  Values on the object inspector are correctly applied to the parent panel.

There may be something wrong with my constructor statement.

Anyways, any help would be appreciated!
Question by:ncnmra
    LVL 27

    Expert Comment

    LVL 33

    Accepted Solution

    hello  ncnmra, , I would not want to have a control on my component and then try an Expose it as that control, as you did -

    property Data: TEditBox read fData write fData;

    although there may be a rare occation that this was nessary, I can not see it,
    I would not expose any properties of the Control and just have my own Component properties to adjust that control , , ,

    not sure if this is what you are asking, but here is a panel component, that may help you to look at -

    unit vPanel;
      SysUtils, Windows, Messages, Classes, stdctrls, Graphics, Controls,
      Forms, ExtCtrls;

      TvPanel = class(TCustomPanel)
        //property Width;
        //property Height;
        FHeading, FData: String;
        FEdit1: TEdit;
        FLabel1: TLabel;
        procedure setHeading(Value: String);
        procedure setData(Value: String);
        property Width;  
    // if you move the base component properties into the protected or private, it will hide them from user in form code
        property Height;
        property Caption;
         constructor Create(AOwner: TComponent); override;
        property Heading: String read FHeading write setHeading;
    // use a setHeading procedure to access the Control's properties
        property Data: String read FData write setData;


    procedure Register;


    constructor TvPanel.Create( AOwner: TComponent );
    inherited Create(AOwner);
    Width := 237;
    Height := 71;
    ControlStyle:= ControlStyle - [csSetCaption];
    FLabel1 := TLabel.Create(Self);
    with FLabel1 do
      Parent := Self;
      Color := $84007B;
      Caption := ' vPanel ';
      Font.Style := Font.Style+[fsBold];
      Font.Color := $E8FFEF;
      AutoSize := True;

    FEdit1 := TEdit.Create(Self);
    with FEdit1 do
      Parent := Self;
      Text := 'No data';
      Font.Name := 'Arial';
    Caption := '';

    procedure TvPanel.setHeading(Value: String);
    widthT: Integer;
    oFont: TFont;
    if Value <> FHeading then
      FHeading := Value;
      oFont := Canvas.Font;
      Canvas.Font := FLabel1.Font;
      widthT := Canvas.TextWidth(' '+FHeading+' ');
      Canvas.Font := oFont;
      FLabel1.Caption := ' '+FHeading+' ';
      FLabel1.Left := (Width div 2) - (widthT div 2);

    procedure TvPanel.setData(Value: String);
    if Value <> FHeading then
      FData := Value;
      FEdit1.Text := FData;

    procedure Register;
    RegisterComponents('Jack', [TvPanel]);


     = = = = = = = = = = = = = = = = = = = = =
    I hope this will help

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Suggested Solutions

    The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
    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…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    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

    18 Experts available now in Live!

    Get 1:1 Help Now