Solved

accessing run time created components

Posted on 1997-09-07
8
147 Views
Last Modified: 2010-04-04
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
Comment
Question by:hdashti
8 Comments
 
LVL 5

Accepted Solution

by:
ronit051397 earned 200 total points
ID: 1344238
use the following command:

with TMemo(FindComponent('memo3')) do
begin
.
.
end;
0
 

Expert Comment

by:davep051797
ID: 1344239
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
 
LVL 4

Expert Comment

by:StevenB
ID: 1344240
 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:hdashti
ID: 1344241
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
 

Author Comment

by:hdashti
ID: 1344242
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
 

Author Comment

by:hdashti
ID: 1344243
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
 
LVL 5

Expert Comment

by:ronit051397
ID: 1344244
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
 

Author Comment

by:hdashti
ID: 1344245
yup that makes sense. thanks for that pointer, wouldnt have guessed that in a long while
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

803 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