Solved

accessing run time created components

Posted on 1997-09-07
8
150 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: 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!

 

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

Technology Partners: 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!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

690 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