Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object Reference On The Fly?

Status
Not open for further replies.

SpeedDemon

Programmer
Jan 1, 2001
70
GB
Hi all, how do you generate an object reference on the fly?

For example, if you have a text box called text1 and another called text2

how could I do the following loop:

for i:=1 to 2 do
begin
text.enabled := False;
end;

Where the loop sets the 'enabled' property of text1 and text2 to false. Please help.

Thanks!

 
You can use an array of pointers to objects to do what you're suggesting.

eg.
Code:
componentArray: Array[1..2] of TEdit;

componentArray[1] := Edit1;
componentArray[2] := Edit2;

for j := 1 to 2 do
     componentArray[j].Enabled := False;
 
Thanks, i'll try it.

Is there any other ways to reference objects directly without the need for an array?

Cheers
 
> Is there any other ways to reference objects directly without the need for an array?

There is an already existing array of objects that you can use. Every component, including your form, has an array of components it owns. You can set the Tag property of each of your Edit controls to some flag value, say 42, and then write code like:
Code:
    for i := 0 to Form1.ComponentCount - 1 do
        if Form1.Components[i].Tag = 42 then
            (Form1.Components[i] as TEdit).Enabled := False;
, and you could make that look substantially less ugly using a "
Code:
with
" or two.

This method is kind of nifty because you can add a third Edit control to your form, set its tag to 42, and it will just work, without you having to add or modify any lines of code. Thus it scales very nicely to dozens of controls.
 
What about

if (form1.Components is TEdit) then ...

You wouldn't need the tags here, unless you want to add another level of checking.

lou
 
Sure; unless the form contains other TEdits that you don't want this special processing to apply to, and/or you want it to apply to objects other than TEdits. For example, it's quite common for a TEdit to have a TLabel to its left; if you wanted to disable those TLabel's at the same time, you'd set their tag to 42 as well.

I use this on a preferences form in one of my apps. When I inherited the application, it had a whole bunch of code to load each preference value from the registry, and another whole bunch to write them back when the user clicked Save. I replaced all this with some code which iterates through each control on the form, and if its tag is 42, reads/writes its value from/to the registry. For different control types it read/writes different data types: check boxes are boolean, spinedits and comboboxes are numeric, etc.

This makes maintenance trivial: to add a new preference, all I do is add a control, give the control the name that I want for the registry value (my code uses the control's Name for the registry value name), and set its tag to 42 -- no additional code required.
 
hi Ronin441

Good thinking, Batman. Like the idea, especially referring to the Tlabel etc.

lou
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top