Solved

accessing run time created components

Posted on 1997-09-07
8
145 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
Comment Utility
use the following command:

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

Expert Comment

by:davep051797
Comment Utility
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
Comment Utility
 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
 

Author Comment

by:hdashti
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:hdashti
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
yup that makes sense. thanks for that pointer, wouldnt have guessed that in a long while
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now