Recent

Author Topic: object name strings  (Read 3389 times)

AnxietyDrive

  • New Member
  • *
  • Posts: 10
object name strings
« on: July 27, 2016, 09:29:18 pm »
I know I could handle this a different way ... perhaps by creating and using an array. However I want to see if I can do it this way .

I have a number of TEdit objects named 'Box_1' ... 'Box_2' ... 'Box_20' etc

By concatanation I can generate a String variable ' currentBox' the value of which is  string name of the TEdit box I wish to interact with.

ie: currentBox = 'Box_5'

Can I and if so how do I call the actual object to interact with it?

lainz

  • Hero Member
  • *****
  • Posts: 4468
    • https://lainz.github.io/
Re: object name strings
« Reply #1 on: July 27, 2016, 09:52:50 pm »
This was answered some weeks ago, try searching the forum.

I don't know if this was the best solution, but at least it works:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   currentBox: string = 'Edit1';
  4.   i: integer;
  5. begin
  6.   for i := 0 to Self.ComponentCount - 1 do
  7.   begin
  8.     if Self.Components[i].Name = currentbox then
  9.       with TEdit(Self.Components[i]) do
  10.       begin
  11.         // do something
  12.         Caption := 'testing..';
  13.       end;
  14.   end;
  15. end;

AnxietyDrive

  • New Member
  • *
  • Posts: 10
Re: object name strings
« Reply #2 on: July 27, 2016, 10:04:52 pm »
Thank you .

shobits1

  • Sr. Member
  • ****
  • Posts: 271
  • .
Re: object name strings
« Reply #3 on: July 27, 2016, 10:23:51 pm »
you can use FindComponent function

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   currentBox: string = 'Edit1';
  4.   te: TEdit;
  5. begin
  6.   te := TEdit(FindComponent(currentBox));
  7.   if Assigned(te) then
  8.   begin
  9.     te.Text := currentBox + ' Founded';
  10.     // do something ....
  11.   end
  12.   else
  13.     ShowMessage(currentBox + ' Not Founded');
  14. end;      
  15.  

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: object name strings
« Reply #4 on: July 27, 2016, 10:38:08 pm »
The attached project gives you a working example of using FindComponent() as shobits1 suggests.

AnxietyDrive

  • New Member
  • *
  • Posts: 10
Re: object name strings
« Reply #5 on: July 28, 2016, 10:57:59 pm »
Many thanks to you all .

 

TinyPortal © 2005-2018