1

I have 100 object of TEdit (for example)

Edit1, Edit2, Edit3... Edit100

How to use a loop to get text from them one by one?

manlio
  • 18,345
  • 14
  • 76
  • 126
Milad.K
  • 11
  • 5
  • you would get a better response if you show some code that you need help on. The StackOverflow community frowns upon questions that seem like homework without any initial effort from the original poster (you in this case) – callisto Jul 05 '16 at 08:53
  • What are you storing these 'TEdit' objects in, e.g. an array, an STL list? What methods do you have on the TEdit object that you want to call? What do you want to do with the data? – LordWilmore Jul 05 '16 at 09:01
  • Take a look at `FindComponentl(componentname)` and then use that with `"Edit1"`, `"Edit2"`, etc.The names can be generated in a loop. – Rudy Velthuis Jul 05 '16 at 09:46
  • 1
    http://stackoverflow.com/questions/11685195/assign-values-to-multiple-edit-boxes-given-their-names – Rudy Velthuis Jul 05 '16 at 10:06
  • @RudyVelthuis Thank you very much for link ;-) – Milad.K Jul 05 '16 at 14:17

3 Answers3

1

I would suggest using an array for that, eg:

class TForm1 : public TForm
{
__published:
    TEdit *Edit1;
    TEdit *Edit2;
    TEdit *Edit3;
    ...
private:
    TEdit* edits[100];
    ...
public:
    __fastcall TForm1(TComponent *Owner);
    ...
};

__fastcall TForm1::TForm1(TComponent *Owner)
{
    for(int i = 0; i < 100; ++i)
    {
        edits[i] = static_cast<TEdit*>(FindComponent("Edit"+IntToStr(i+1)));
    }
}

...

for(int i = 0; i < 100; ++i)
{
    //  use edits[i]->Text as needed...
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

Something like this should work:

for (int i = 0; i < form->ControlCount; ++i)
{
  TEdit *edit = dynamic_cast<TEdit *>(form->Controls[i]);

  if (edit)
    ShowMessage(edit->Text);        
}

The code is based on the dynamic_cast conversion: if the cast fails it returns a null pointer and you can skip the current control.

manlio
  • 18,345
  • 14
  • 76
  • 126
  • This would only work as shown if the `TEdit` controls are *direct* children of the Form itself and not some other `Parent` container on the Form, like a Panel, Frame, TabSheet, etc. And also, this loops through ALL available `TEdit` controls, but what if the OP is only interested in a subset of them instead? And also, this is only taking creation order into account, not naming order. – Remy Lebeau Jul 05 '16 at 19:02
  • @RemyLebeau Well, it's true but the snippet is just a "proof of concept". It can be extended with recursion to handle nested controls (filtering the results is quite easy). Indeed the tree traversal order could be an issue. – manlio Jul 06 '16 at 11:09
0
TEdit *tmpEdit = NULL ;
vector <AnsiString> tmpStr ;
for(int i = 0; i < 100; ++i)
{
    tmpEdit = static_cast<TEdit*>(FindComponent("Edit"+IntToStr(i+1)));
    tmpStr.push_back(tmpEdit->Text) ;
}