Solved

Array of components property

Posted on 2001-09-09
2
135 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

22 Experts available now in Live!

Get 1:1 Help Now