问题
i am currently doing some programming in Borland C++ Builder 6.
I have 24 edit boxes(a visual component, with a text field) and i want to insert some values in the boxes, now i do it like this:
Edit1->Text=1;
Edit2->Text=2;
Edit3->Text=3;
...
Edit24->Text=24;
but i want to have something like this:
for(int i=1; i<25;i++){
Edit"i"->Text=i;
}
i think i have to make an array of objects or something. Can any body help me with this? I don't have a lot of experience with objects and stuff like that.
回答1:
There is a FindComponent
function in VCL. It is used to find a component by it's name.
In your case it will look something like:
TEdit * tmp;
for( int i = 0; i < 24; i ++ )
{
tmp = (TEdit*)MyForm->FindComponent("Edit" + IntToStr(i) );
tmp->Text = i;
}
来源:https://stackoverflow.com/questions/11685195/assign-values-to-multiple-edit-boxes-given-their-names