Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 153
  • Last Modified:

accessing run time created components

how do i access and manipulate components created at run time? ie new instances of Tmemo are created with the click of a button.  When the mouse is clicked on an individual Tmemo which say a name memo3, how do i then get access to that component to changes the font/color/pen/etc?
0
hdashti
Asked:
hdashti
1 Solution
 
ronit051397Commented:
use the following command:

with TMemo(FindComponent('memo3')) do
begin
.
.
end;
0
 
davep051797Commented:
There are a few ways of going about this. The easiest and cleanest way is to var an array of the object ahead of time. then you can easily refrerence it by the index of the array. For example:
                var mymemo:array[1..20] of Tmemo;
                  d:integer;
                   begin
                     for d:=1 to 10 do
                         begin
                          mymemo[d]:=Tmemo.create(self);
                                   with mymemo[d] do
                                        begin
                                          parent:=self;
                                           top:=100;
                                           left:=d*50;
                                          end;
                              end;  

Now in a buttonclick event you may want to change a property of one of the Memos... for this you would just access it by its index number:
                                                         
                               mymemo[8].left:=25;
               
       Thats all there is to it.
                                              Good luck,
                                                        Dave                  
0
 
StevenBCommented:
 And if you don't want a fixed array then you can add the components to a dynamic TList
 [ AList := TList.Create;
   AList.Add(AComponent); ]

  and then refer back to the components with something like:

  AComponent := AList[8];

  Steven.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
hdashtiAuthor Commented:
ronit godbless that is what i was looking for FindComponent, so i can use it with STevenBs, technique which i tried but keep getting memory erros all the time, and that should work. hopefully.
0
 
hdashtiAuthor Commented:
hmm interesting have done the above and works like a beauty, except for when it comes to removing it

ie.
for i:=0 to ComponentCount -1 do
 begin

   If (Component[i] is Tmemo) then
           TMemo(Component[i]).free; or even
           Component[i].free
   end

it crashed when the last component is accessed. i can get it down to the last component of a previously empty form, but then the whole thing gives a memory access error. if i delete it manually each component at a time,it seems to be ok. any ideas?

worth 300 points i guess
0
 
hdashtiAuthor Commented:
stevenB is concept of the Tlist never knew that existed in delphi i guess it so0mehting like the Vectors() in java good one. will use that.

thankyou
0
 
ronit051397Commented:
This has nothing to do with creating components on the fly.
You cannot use for I:=..., beacuse if you free a component then
the componentcount decreases and you'll get maybe 'list index out
of bounds', that make sense when you are talking about the last component.

If the memo's are the last components that were created, therefore have the highest indexes then you can write the following instead:
while (Components[ComponentCount-1] is TMemo do
  Components[ComponentCount-1].Free;
0
 
hdashtiAuthor Commented:
yup that makes sense. thanks for that pointer, wouldnt have guessed that in a long while
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now