Solved

accessing run time created components

Posted on 1997-09-07
8
148 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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