Solved

Array of components property

Posted on 2001-09-09
2
138 Views
Last Modified: 2010-04-06
Hi,

I have 2 components that I am writing, let's call them TComponent1 and TComponent2.

Now TComponent1 needs to have an array of TComponent2's as a property that can be indexed.  The closest thing that I can think of in Delphi's VCL to this is the TPopupMenu and TMenuItem, or TPageControl and TTabSheet.

There needs to be an easy way for the user of the components to...

* Interate through the array as a property in TComponent1 to access the TComponent2s.
* Allow instances of TComponent2 to be Added to the array property in TComponent1.
* Delete instances from the array property.
* Change the order of the items in the array property.
* Count the number of items in the array property.

I assume I would need a linked list somewhere (TList).  Would anyone be able to give me any source code that would demonstrate the above?

Thanks in Advance,

Richard
0
Comment
Question by:Richard2000
2 Comments
 
LVL 6

Expert Comment

by:zebada
ID: 6469171
Best to use TObjectList and set the OwnsObjects property to true.
0
 
LVL 14

Accepted Solution

by:
DragonSlayer earned 50 total points
ID: 6469543
type
  MyComp2 = class
    // some stuff here
  end;

  MyComp1 = class
  private
    FComp2List: TStringList;
  protected
    function GetListCount: Integer;
    function GetComp2(Index: Integer): MyComp2;
    function GetComp2ByName(Diz: string): MyComp2;
  public
    constructor Create;
    destructor Destroy;
    function Add(const Diz: string;
      Comp2: MyComp2): Integer;

    property Comp2ListCount: Integer read GetListCount;
    property Comp2[Index: Integer]: MyComp2 read GetComp2;
    property Comp2ByName[Diz: string]: MyComp2 read GetComp2ByName;
  end;

begin

constructor MyComp1.Create;
begin
  FComp2List := TStringList.Create;
  FComp2List.Sorted := True;
end;

destructor MyComp1.Destroy;
var
  i: Integer;
begin
  if FComp2List.Count > 0 then
    for i := FComp2List.Count - 1 downto 0 do
    begin
      MyComp2(FComp2List.Objects[i]).Free;
      FComp2List.Delete(i);
    end;
  FComp2List.Free;
end;

function MyComp1.GetListCount: Integer;
begin
  Result := FComp2List.Count;
end;

function MyComp1.GetComp2(Index: Integer): MyComp2;
begin
  Result := nil;
  if (Index > -1) and (Index < FComp2List.Count) then
    Result := MyComp2(FComp2List.Objects[Index])
  // you can also raise an exception if you want to.
  // just uncomment the following line
//  else raise Exception.Create('Invalid index reference');
end;

function MyComp1.GetComp2ByName(Diz: string): MyComp2;
begin
  Result := GetComp2(FComp2List.IndexOf(Diz));
end;

function MyComp1.Add(const Diz: string; Comp2: MyComp2): Integer;
begin
  Result := FComp2List.AddObject(Diz, Comp2);
end;




Hope that helps!


DragonSlayer.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

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

679 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