Solved

Array of components property

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 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