Solved

Array of components property

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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…

786 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