#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Component Creation at Runtime


    I am creating a component at runtime but i am having an issue because when I create 2 of these components, I would change the value of the properties on one of them but it seems to also change it on the other.

    How can I create components at runtime so they are seperate components and not instances of eachother?

    Thanks,

    Code Ive got so far

    Code:
    Cell[CellCount]:= TBattery.Create(nil);
    Cell[CellCount].Top := Random(500);
    Cell[CellCount].Left := Random(500);
    Cell[CellCount].Parent := Self;
    Cell[CellCount].ID := CellCount;
    CellCount := CellCount + 1;
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2008
    Posts
    55
    Rep Power
    6

    Thumbs up


    When using CREATE( OWNER ) parameter, the OWNER will be who will delete the new component. When using NIL, will be you who delete. When using SELF, usually, will be "the FORM", or, another container (FORM, FRAME, DATAMODULE, PANEL, etc...)
    --------------------------------------

    var
    xCmpTmp:TButton;
    begin
    try
    try
    xCmpTmp := TButton.create;
    xCmpTmp.propertyXXXX := valueXXXXX;
    except
    ... exception control here
    end;
    finally

    { if Assigned(xCmpTmp) then FreeAndNil(xCmpTmp); // if dont need more }

    end;

    if Needs of the component in another place in your software, MAKE IT not in a handler event, procedures, functions etc... MAKE IT in a place BEFORE use it and DELETE it in another place

    example:

    1 - make it before called the forms that using it
    2 - erase (free) it after all use this components.

    You can use a ARRAY for stored your components:

    var
    xCmpTmpArray:Array[0..10] of TButton;

    ....
    xCmpTmpArray[0] := TButton.Create;

    For use one "OPEN ARRAY" ( xCmpTmpArray:Array of TButton) you need one code more complex to control "what" will deleted.

    Making components in RunTime
    http://delphi.about.com/od/kbcurt/a/dynamiccreation.htm
    Last edited by emailx45; March 26th, 2013 at 11:49 AM.

IMN logo majestic logo threadwatch logo seochat tools logo