如何通过控件命名来遍历

csdn_tw 2006-09-30 06:54:15
有10个Label控件,其Name属性分别为“Label1、Label2、Label3...Label10”,想在for语句中通过其Name属性遍历,如何实现?
...全文
207 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
maple_2500 2006-10-09
  • 打赏
  • 举报
回复
此程序将遍历在控件名循环放入ListView中:
TListItem *ListItem;
ListView1->Items->Clear();
for(int i=0;i<this->ComponentCount;i++){
if(dynamic_cast<TLabel *>(this->Components[i])){
TLabel *temp = dynamic_cast<TLabel *>(this->Components[i]);
ListItem = ListView1->Items->Add();
ListItem->Caption = temp->Name;
}
}
wood542344 2006-10-08
  • 打赏
  • 举报
回复
const int EditBoxCount = 20;

const int LeftCoordinate = 10;
TForm1 *Form1;
TEdit* pe[20];

void __fastcall TForm1::FormCreate(TObject *Sender)

{
const char* pszNamePrefix = "MyEdit";
for (int i=0;i<EditBoxCount;i++)
{
pe[i] = new TEdit(this);
pe[i]->Name = pszNamePrefix + IntToStr(i+1);
pe[i]->Left = LeftCoordinate;
pe[i]->Top = i*EditBoxCount;
pe[i]->Parent = this;
}

delete pszNamePrefix;
}

void __fastcall TForm1::FormDestroy(TObject *Sender)

{
for (int i=0;i<EditBoxCount;i++)
delete pe[i];
}
帮助里的例子。。。。
flowercity 2006-10-03
  • 打赏
  • 举报
回复
dynamic_cast
samchoy 2006-10-03
  • 打赏
  • 举报
回复
TComponent* __fastcall FindComponent(const AnsiString AName);
牧牛人软件 2006-10-01
  • 打赏
  • 举报
回复
mark
柯本 2006-10-01
  • 打赏
  • 举报
回复
当然可以实现,以下是将Label1-10的Caption改为"This is Label 1..10"的Ex:
String x;
TLabel *t;
for (int i=1;i<=10;i++) //Label1-10
{
x.printf("Label%d",i);
t= (TLabel *)FindComponent(x);
if (t!=NULL)
{
x.printf("This is Label %d",i);
t->Caption=x;
//在这你可改Label 1-10的其它属性
}
}
ribut9225 2006-10-01
  • 打赏
  • 举报
回复
支持楼上的
遍历所有控件的NAME,如果NAME的前五位是label,则可以进行处理
否则不处理

wz 2006-10-01
  • 打赏
  • 举报
回复
可以考虑将需遍历的控件放在Panel上,通过其Controls访问。
我来看看CB 2006-10-01
  • 打赏
  • 举报
回复
不好办。

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧