读书人

怎么用循环语句给多个标签赋值

发布时间: 2012-08-15 16:57:16 作者: rapoo

如何用循环语句给多个标签赋值
我将一个字符串拆分成多个子字符串后分别赋值给多个标签,当子字符串数量固定时,我用如下代码一个一个的赋值,
可是子字符串数量不定,比如可能是10-30间任意值时,如何用循环语句给多个标签赋值呢?
pt1-> Count可以求出子字符串数量。



C/C++ code
TStringList   *pt1   =   new   TStringList; pt1->Delimiter  = ' '; pt1->DelimitedText  = Edit29->Text;  Label73->Caption= pt1->Strings[0]; Label74->Caption= pt1->Strings[1]; Label75->Caption= pt1->Strings[2]; Label76->Caption= pt1->Strings[3]; Label77->Caption= pt1->Strings[4]; Label78->Caption= pt1->Strings[5]; Label79->Caption= pt1->Strings[6]; Label80->Caption= pt1->Strings[7]; Label81->Caption= pt1->Strings[8]; Label82->Caption= pt1->Strings[9]; Label83->Caption= pt1->Strings[10]; Label84->Caption= pt1->Strings[11]; Label85->Caption= pt1->Strings[12]; Label86->Caption= pt1->Strings[13]; Label87->Caption= pt1->Strings[14]; Label88->Caption= pt1->Strings[15]; Label89->Caption= pt1->Strings[16]; Label90->Caption= pt1->Strings[17]; Label91->Caption= pt1->Strings[18]; Label92->Caption= pt1->Strings[19]; Label93->Caption= pt1->Strings[20]; Label94->Caption= pt1->Strings[21]; Label95->Caption= pt1->Strings[22]; Label96->Caption= pt1->Strings[23]; Label97->Caption= pt1->Strings[24]; delete pt1;


[解决办法]
for(int i = 0; i < pt1->Count; i++)
{
String str = "..."; 你要赋值的控件的名字
TLabel *label = (TLabel *)(FindComponent(str));
label->Caption = pt1->Strings[i];
}
[解决办法]
刚细看了看,我在2楼的代码确实有瑕疵。重新改进一下:
C/C++ code
TStringList *pt1 = new TStringList;pt1->Delimiter = ' ';pt1->DelimitedText = Edit29->Text;TLabel *lbl;String str;for (int i = 0, j = 0; i < 100; i++){    str.sprintf(TEXT("Label%d"), i);    lbl = (TLabel *)FindComponent(str);    if (lbl && j < pt1->Count)    {        lbl->Caption = pt1->Strings[j++];    }}delete pt1; 

读书人网 >C++ Builder

热点推荐