Go Premium for a chance to win a PS4. Enter to Win

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

Accessing components dynamically

Hiya,

Is there a way in D2 to access an existing component on a form given a string that contains the name of the component?
I'm not clear that something like:
var
  cc: TPersistentClass ;
  c: TComponent;
begin
  cc := GetClass('Memo1');
  c := TComponentClass(cc).Create(application);

is appropriate because the Memo1 component was already created with the form create.  Is there a simple way.
A code snippet would be much appreciated.

Thanks,
Bob Skinner
rskinner@ix.netcom.com
component := TComponentClass(
0
skin444
Asked:
skin444
1 Solution
 
icampbe1Commented:
Every component has an entry in the forms Components array.   Loop through the array looking for the component with the same name (the name property).

For x := 0 to componentCount - 1 DO
  IF form.components[x].name = 'xxxx' THEN Break;

If you want the search by class then every object that descends from TObject (thats about all of them) can reference the ClassName property of the object instead of the Name property.

You might have to fool around with the syntax here as I haven't actually coded your solution.  ie. typecast the components array entry.

Try it and let me know.

Cheers.

0
 
lgc5800Commented:
For access dynamically component you can see the sample from
BORLAND in the folder DEMO of DELPHI 1.2 (16bits)
The name of the .DPR is DYNAINST.DPR




0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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