Delphi

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • Component or Frame Arrays

    5 answers - 2910 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    I have created a frame which comprises a panel on which are mounted 2 other
    panels and a drop down combo box. (The combobox and 2 child frames are
    arranged horizontally in line so that the whole item appears as a single
    line)
    My plan was to create an instance of this frame at run time for each line I
    read from a file , set the Caption property of the child panels and add a
    number of items to the combo. I have exposed the frame's sub components as
    properties and set each subcomponent to true.
    The only way I have managed to create a set of these frames at run time is
    during the main form's create process. Even then I cannot seem to be able to
    access these frames or their sub-components.
    This is my main program code
    unit PICFuseConfigurator;
    interface
    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, ExtCtrls, PICFuseItem;
    type
    TfmFuseConfig = class(TForm)
    Panel1: TPanel;
    procedure FormCreate(Sender: T);
    procedure FormPaint(Sender: T);
    private
    { Private declarations }
    public
    { Public declarations }
    procedure AddFuseItem(pIndex: Integer);
    end;
    var
    fmFuseConfig: TfmFuseConfig;
    FuseList: TFrame;
    implementation
    {$R *.dfm}
    procedure TfmFuseConfig.FormCreate(Sender: T);
    Var i: Integer;
    begin
    //
    for i := 0 to 8 do
    begin
    AddFuseItem(i);
    end;
    end;
    procedure TfmFuseConfig.AddFuseItem(pIndex: Integer);
    begin
    FuseList := TFrmFuse.Create(Self);
    with FuseList do
    begin
    Parent := fmFuseConfig;
    Visible := True;
    Name := 'FuseList' + IntToStr(pIndex);
    Align := alTop;
    end;
    end;
    end.
    This is my Frame's code
    unit PICFuseItem;
    interface
    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls, ExtCtrls;
    type
    TfrmFuse = class(TFrame)
    pnlFuseEntry: TPanel;
    cmbValue: TComboBox;
    pnlName: TPanel;
    pnlDescription: TPanel;
    private
    { Private declarations }
    public
    { Public declarations }
    Constructor Create(A: TComponent);
    Published
    property ItemName: TPanel Read pnlName Write pnlName;
    property ItemValues: TComboBox Read cmbValue Write cmbValue;
    property ItemDesc: TPanel Read pnlDescription Write pnlDescription;
    end;
    implementation
    {$R *.dfm}
    Constructor TfrmFuse.Create(A: TComponent);
    begin
    inherited;
    pnlName.SetSubComponent(True);
    cmbValue.SetSubComponent(True);
    pnlDescription.SetSubComponent(True);
    end;
    end.
    Is it possible to build up arrays of controls in Delphi or am I trying to do
    something inherantly impossible.
    JohnB
    Delphi mailing list -Delphi (AT) elists (DOT) org
  • No.1 | | 3173 bytes | |

    K Thsnks for your help, I can now add the frames at runtime
    I am adding them in the formShow event at the moment just for testing.

    I still can't access the frame's sub components.
    As you can see I am setting the subcomponents true.
    How do I access these properties from my main form?

    In my main form show event I load up 8 instances of the frame:

    procedure TfmFuseConfig.FormShow(Sender: T);
    Var i: Integer;
    begin
    for i := 0 to 7 do
    AddFuseItem(i);
    end;

    procedure TfmFuseConfig.AddFuseItem(pIndex: Integer);
    begin
    FuseList := TFrmFuse.Create(Self);
    with FuseList do
    begin
    Parent := Self;
    Visible := True;
    Align := alTop;
    Name := 'FuseNo' + IntToStr(pIndex);
    end;
    end;

    This leaves me with 8 frames visible on my form, I assume with Names of
    FuseNo0 throught to FuseNo7.
    How do I access these frames and their sub components? I have tried
    populating their properties in the AddFuseItem procedure but I just get a
    syntax error saying undeclared identifier.

    E.g.

    procedure TfmFuseConfig.AddFuseItem(pIndex: Integer);
    begin
    FuseList := TFrmFuse.Create(Self);
    with FuseList do
    begin
    Parent := Self;
    Visible := True;
    Align := alTop;
    Name := 'FuseNo' + IntToStr(pIndex);
    ItemDesc.Caption := Name;// this line errors
    end;
    end;

    This is the source for the
    unit PICFuseItem;

    interface

    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

    Dialogs, StdCtrls, ExtCtrls;

    type
    TfrmFuse = class(TFrame)
    pnlFuseEntry: TPanel;
    cmbValue: TComboBox;
    pnlName: TPanel;
    pnlDescription: TPanel;
    private
    { Private declarations }
    public
    { Public declarations }
    Constructor Create(A: TComponent);
    property ItemName: TPanel Read pnlName Write pnlName;
    property ItemValues: TComboBox Read cmbValue Write cmbValue;
    property ItemDesc: TPanel Read pnlDescription Write pnlDescription;
    end;

    implementation

    {$R *.dfm}
    Constructor TfrmFuse.Create(A: TComponent);
    begin
    inherited;
    pnlName.SetSubComponent(True);
    cmbValue.SetSubComponent(True);
    pnlDescription.SetSubComponent(True);
    end;

    end.

    Message
    From: delphi-bounces (AT) elists (DOT) org [mailto:delphi-bounces (AT) elists (DOT) org] Behalf
    zayin
    Sent: 30 June 2007 02:10
    To: 'Borland's Delphi Discussion List'
    Subject: RE: Component or Frame Arrays

    ,

    And I forgot. As to the array.

    Sure you can make an array if you want, static if you know that bound or
    dynamic if not.

    a TList. That is what I would use.

    var
    frameList:TList;

    frameList:=TList.Create;

    loop

    FuseList := TFrmFuse.Create(Self);
    frameList.Add(FuseList);
    with FuseList do

    And in the destroy or close of the form, walk the frameList and free all the
    frames.

    Ciao,

    Mark

    Delphi mailing list -Delphi (AT) elists (DOT) org

    Delphi mailing list -Delphi (AT) elists (DOT) org
  • No.2 | | 735 bytes | |

    At 09:32 PM 02/07/07, John Barrat wrote:


    >This leaves me with 8 frames visible on my form, I assume with Names of
    >FuseNo0 throught to FuseNo7.
    >How do I access these frames and their sub components? I have tried
    >populating their properties in the AddFuseItem procedure but I just get a
    >syntax error saying undeclared identifier.


    what about an array of TFuseItem in the form.

    var
    MyFuseArray : array[18] of TFuseItem.

    When you create it you can do something like
    MyFuseArray[i] := TFuseItem.Create

    I do things like this quite regularly.

    Cheers,
    Chris.

    Delphi mailing list -Delphi (AT) elists (DOT) org
  • No.3 | | 3145 bytes | |

    John Barrat wrote:
    K Thsnks for your help, I can now add the frames at runtime
    I am adding them in the formShow event at the moment just for testing.

    I still can't access the frame's sub components.
    As you can see I am setting the subcomponents true.
    How do I access these properties from my main form?

    In my main form show event I load up 8 instances of the frame:

    procedure TfmFuseConfig.FormShow(Sender: T);
    Var i: Integer;
    begin
    for i := 0 to 7 do
    AddFuseItem(i);
    end;

    procedure TfmFuseConfig.AddFuseItem(pIndex: Integer);
    begin
    FuseList := TFrmFuse.Create(Self);
    with FuseList do
    begin
    Parent := Self;
    Visible := True;
    Align := alTop;
    Name := 'FuseNo' + IntToStr(pIndex);
    end;
    end;

    This leaves me with 8 frames visible on my form, I assume with Names of
    FuseNo0 throught to FuseNo7.
    How do I access these frames and their sub components?

    Right now, you're storing a reference to each new frame in some variable
    named FuseList. At the end of your loop, that will contain only a
    reference to the last frame you created. If you want to keep references to
    all of them, use an array or a list.

    , you can only access them indirectly. You can use the form's
    FindComponent function to find something by name, or you can iterate over
    the Components or Controls arrays searching for whatever frame you're
    looking for.

    I have tried
    populating their properties in the AddFuseItem procedure but I just get a
    syntax error saying undeclared identifier.

    E.g.

    procedure TfmFuseConfig.AddFuseItem(pIndex: Integer);
    begin
    FuseList := TFrmFuse.Create(Self);

    Does that line actually compile? Mark declared FuseList as a TList, not
    any superclass of TFrmFuse. What is FuseList in your code?

    with FuseList do
    begin
    Parent := Self;
    Visible := True;
    Align := alTop;
    Name := 'FuseNo' + IntToStr(pIndex);
    ItemDesc.Caption := Name;// this line errors
    end;
    end;

    Remove the "with" block. Write everything out. Does the error still occur
    on the same line?

    This is the source for the

    unit PICFuseItem;

    interface

    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
    Forms,

    Dialogs, StdCtrls, ExtCtrls;

    type
    TfrmFuse = class(TFrame)
    pnlFuseEntry: TPanel;
    cmbValue: TComboBox;
    pnlName: TPanel;
    pnlDescription: TPanel;
    private
    { Private declarations }
    public
    { Public declarations }
    Constructor Create(A: TComponent);

    Surely that's supposed to be marked as "override."

    property ItemName: TPanel Read pnlName Write pnlName;
    property ItemValues: TComboBox Read cmbValue Write cmbValue;
    property ItemDesc: TPanel Read pnlDescription Write pnlDescription;

    Those properties are simply providing alias names for the component
    fields. The fields are already published. You don't need to access the
    components via the properties since anyone can access them via the fields
    already.
  • No.4 | | 5110 bytes | |

    You need to keep some list (and an array is a list) of pointers to the TFrmFuse objects you are
    creating or you cannot reference them.
    So if we combine your code and some of the examples below

    var
    LFuseItems:TList;

    procedure TfmFuseConfig.FormShow(Sender: T);
    Var i: Integer;
    begin
    LFuseItems:=TList.Create;
    for i := 0 to 7 do
    LFuseItems.Add(AddFuseItem(i)); //add the pointer to the new FuseItem
    end;
    end;

    function TfmFuseConfig.AddFuseItem(pIndex: Integer): TFrmFuse;
    //return the pointer to what you create
    var
    NewFuse : TFrmFuse;
    begin
    NewFuse := TFrmFuse.Create(Self);
    with NewFuse do begin
    Parent := Self;
    Visible := True;
    Align := alTop;
    Name := 'FuseNo' + IntToStr(pIndex);
    end;
    result := NewFuse; //this returns the pointer to what you just created
    end;

    Now you can refer to any of your TFrmFuse objects with (example references i-th one)
    TFrmFuse(LFuseItems[i])
    and
    TFrmFuse(LFuseItems[i]).ItemName
    will refer to the ItemName TPanel on the i-th TFrmFuse

    Last time I did this I used the equivalent of
    NewFuse := TFrmFuse.Create(nil);
    and then freed them all in my own destructor

    HTH
    Wayne

    Borland's Delphi Discussion List <delphi (AT) elists (DOT) orgon Monday, 2 July 2007 at 11:32 p.m. +0000
    wrote:
    >K Thsnks for your help, I can now add the frames at runtime
    >I am adding them in the formShow event at the moment just for testing.
    >
    >I still can't access the frame's sub components.
    >As you can see I am setting the subcomponents true.
    >How do I access these properties from my main form?
    >
    >In my main form show event I load up 8 instances of the frame:
    >
    >procedure TfmFuseConfig.FormShow(Sender: T);
    >Var i: Integer;
    >begin
    >for i := 0 to 7 do

    AddFuseItem(i);
    >end;
    >
    >procedure TfmFuseConfig.AddFuseItem(pIndex: Integer);
    >begin
    >FuseList := TFrmFuse.Create(Self);
    >with FuseList do

    begin
    Parent := Self;
    Visible := True;
    Align := alTop;
    Name := 'FuseNo' + IntToStr(pIndex);
    end;
    >end;
    >
    >This leaves me with 8 frames visible on my form, I assume with Names of
    >FuseNo0 throught to FuseNo7.
    >How do I access these frames and their sub components? I have tried
    >populating their properties in the AddFuseItem procedure but I just get a
    >syntax error saying undeclared identifier.
    >
    >E.g.
    >
    >procedure TfmFuseConfig.AddFuseItem(pIndex: Integer);
    >begin
    >FuseList := TFrmFuse.Create(Self);
    >with FuseList do

    begin
    Parent := Self;
    Visible := True;
    Align := alTop;
    Name := 'FuseNo' + IntToStr(pIndex);
    ItemDesc.Caption := Name;// this line errors
    end;
    >end;
    >
    >
    >
    >This is the source for the
    >unit PICFuseItem;
    >
    >interface
    >
    >uses

    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

    Dialogs, StdCtrls, ExtCtrls;
    >
    >type

    TfrmFuse = class(TFrame)
    pnlFuseEntry: TPanel;
    cmbValue: TComboBox;
    pnlName: TPanel;
    pnlDescription: TPanel;
    private
    { Private declarations }
    public
    { Public declarations }
    Constructor Create(A: TComponent);
    property ItemName: TPanel Read pnlName Write pnlName;
    property ItemValues: TComboBox Read cmbValue Write cmbValue;
    property ItemDesc: TPanel Read pnlDescription Write pnlDescription;
    end;
    >
    >implementation
    >
    >{$R *.dfm}
    >Constructor TfrmFuse.Create(A: TComponent);
    >begin

    inherited;
    pnlName.SetSubComponent(True);
    cmbValue.SetSubComponent(True);
    pnlDescription.SetSubComponent(True);
    >end;
    >
    >end.
    >

    Message
    >From: delphi-bounces (AT) elists (DOT) org [mailto:delphi-bounces (AT) elists (DOT) org] Behalf

    zayin
    >Sent: 30 June 2007 02:10
    >To: 'Borland's Delphi Discussion List'
    >Subject: RE: Component or Frame Arrays
    >
    >,
    >
    >And I forgot. As to the array.
    >
    >Sure you can make an array if you want, static if you know that bound or
    >dynamic if not.
    >

    a TList. That is what I would use.
    >
    >var

    frameList:TList;
    >
    >frameList:=TList.Create;
    >
    >loop
    >
    >FuseList := TFrmFuse.Create(Self);
    >frameList.Add(FuseList);
    >with FuseList do
    >
    >
    >And in the destroy or close of the form, walk the frameList and free all the
    >frames.
    >
    >Ciao,
    >
    >Mark
    >


    Delphi mailing list -Delphi (AT) elists (DOT) org
  • No.5 | | 5724 bytes | |

    Thanks for all your input. I have decided that this approach is ending up
    very clumsy and its pretty slow to paint. I have now switched to the TMS
    StringGrid control which is doing everything I want and more.

    Should have gone with that at the start really!

    JohnB

    Message
    From: delphi-bounces (AT) elists (DOT) org [mailto:delphi-bounces (AT) elists (DOT) org] Behalf
    Wayne Roser
    Sent: 03 July 2007 22:43
    To: Borland's Delphi Discussion List
    Cc: 'Borland's Delphi Discussion List'
    Subject: Re: Component or Frame Arrays

    You need to keep some list (and an array is a list) of pointers to the
    TFrmFuse objects you are creating or you cannot reference them.
    So if we combine your code and some of the examples below

    var
    LFuseItems:TList;

    procedure TfmFuseConfig.FormShow(Sender: T); Var i: Integer; begin
    LFuseItems:=TList.Create;
    for i := 0 to 7 do
    LFuseItems.Add(AddFuseItem(i)); //add the pointer to the new
    FuseItem
    end;
    end;

    function TfmFuseConfig.AddFuseItem(pIndex: Integer): TFrmFuse; //return the
    pointer to what you create var
    NewFuse : TFrmFuse;
    begin
    NewFuse := TFrmFuse.Create(Self);
    with NewFuse do begin
    Parent := Self;
    Visible := True;
    Align := alTop;
    Name := 'FuseNo' + IntToStr(pIndex);
    end;
    result := NewFuse; //this returns the pointer to what you just created
    end;

    Now you can refer to any of your TFrmFuse objects with (example references
    i-th one)
    TFrmFuse(LFuseItems[i])
    and
    TFrmFuse(LFuseItems[i]).ItemName
    will refer to the ItemName TPanel on the i-th TFrmFuse

    Last time I did this I used the equivalent of NewFuse :=
    TFrmFuse.Create(nil); and then freed them all in my own destructor

    HTH
    Wayne

    Borland's Delphi Discussion List <delphi (AT) elists (DOT) orgon Monday, 2 July 2007
    at 11:32 p.m. +0000
    wrote:
    >K Thsnks for your help, I can now add the frames at runtime I am
    >adding them in the formShow event at the moment just for testing.
    >
    >I still can't access the frame's sub components.
    >As you can see I am setting the subcomponents true.
    >How do I access these properties from my main form?
    >
    >In my main form show event I load up 8 instances of the frame:
    >
    >procedure TfmFuseConfig.FormShow(Sender: T); Var i: Integer;
    >begin for i := 0 to 7 do

    AddFuseItem(i);
    >end;
    >
    >procedure TfmFuseConfig.AddFuseItem(pIndex: Integer); begin FuseList :=
    >TFrmFuse.Create(Self); with FuseList do

    begin
    Parent := Self;
    Visible := True;
    Align := alTop;
    Name := 'FuseNo' + IntToStr(pIndex);
    end;
    >end;
    >
    >This leaves me with 8 frames visible on my form, I assume with Names of
    >FuseNo0 throught to FuseNo7.
    >How do I access these frames and their sub components? I have tried
    >populating their properties in the AddFuseItem procedure but I just get
    >a syntax error saying undeclared identifier.
    >
    >E.g.
    >
    >procedure TfmFuseConfig.AddFuseItem(pIndex: Integer); begin FuseList :=
    >TFrmFuse.Create(Self); with FuseList do

    begin
    Parent := Self;
    Visible := True;
    Align := alTop;
    Name := 'FuseNo' + IntToStr(pIndex);
    ItemDesc.Caption := Name;// this line errors
    end;
    >end;
    >
    >
    >
    >This is the source for the
    >unit PICFuseItem;
    >
    >interface
    >
    >uses

    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
    >Forms,
    >

    Dialogs, StdCtrls, ExtCtrls;
    >
    >type

    TfrmFuse = class(TFrame)
    pnlFuseEntry: TPanel;
    cmbValue: TComboBox;
    pnlName: TPanel;
    pnlDescription: TPanel;
    private
    { Private declarations }
    public
    { Public declarations }
    Constructor Create(A: TComponent);
    property ItemName: TPanel Read pnlName Write pnlName;
    property ItemValues: TComboBox Read cmbValue Write cmbValue;
    property ItemDesc: TPanel Read pnlDescription Write pnlDescription;
    end;
    >
    >implementation
    >
    >{$R *.dfm}
    >Constructor TfrmFuse.Create(A: TComponent); begin

    inherited;
    pnlName.SetSubComponent(True);
    cmbValue.SetSubComponent(True);
    pnlDescription.SetSubComponent(True);
    >end;
    >
    >end.
    >

    Message
    >From: delphi-bounces (AT) elists (DOT) org [mailto:delphi-bounces (AT) elists (DOT) org]
    >Behalf zayin
    >Sent: 30 June 2007 02:10
    >To: 'Borland's Delphi Discussion List'
    >Subject: RE: Component or Frame Arrays
    >
    >,
    >
    >And I forgot. As to the array.
    >
    >Sure you can make an array if you want, static if you know that bound
    >or dynamic if not.
    >

    a TList. That is what I would use.
    >
    >var

    frameList:TList;
    >
    >frameList:=TList.Create;
    >
    >loop
    >
    >FuseList := TFrmFuse.Create(Self);
    >frameList.Add(FuseList);
    >with FuseList do
    >
    >
    >And in the destroy or close of the form, walk the frameList and free
    >all the frames.
    >
    >Ciao,
    >
    >Mark
    >


    Delphi mailing list -Delphi (AT) elists (DOT) org

    Delphi mailing list -Delphi (AT) elists (DOT) org

Re: Component or Frame Arrays


max 4000 letters.
Your nickname that display:
In order to stop the spam: 0 + 0 =
QUESTION ON "Delphi"

EMSDN.COM