Solved

Array of components property

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Firewall issues 6 85
Strange message in Delphi 10 Seattle 1 463
delphi custom sort exception 6 111
How to use Hashing sha1 in Delphi 2010 4 162
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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