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?
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?
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...
}
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.
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) ;
}