怎样知道Form上控件的类型?

YAMABACK 2005-01-12 03:39:51
遍历Form上的Controls,怎样知道Controls[i]是什么控件?
...全文
76 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Maconel 2005-01-12
  • 打赏
  • 举报
回复
哈,咱俩都来晚了啊。
我不懂电脑 2005-01-12
  • 打赏
  • 举报
回复
void __fastcall TForm1::FormCreate(TObject *Sender)

{
for (int i = 0; i < ComponentCount; i++)
ListBox1->Items->InsertObject(0,
Components[i]->Name,
(TObject *)Components[i]);

}

void __fastcall TForm1::ListBox1MouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)

{
if (Button == mbRight)
{
TClass ClassRef;
int Index = ListBox1->ItemAtPos(Point(X,Y), true);
// only components that are controls have a position
// make sure the component is a control
for (ClassRef = ListBox1->Items->Objects[Index]->ClassType();
ClassRef != NULL;
ClassRef = ClassRef->ClassParent())
if (String(ClassRef->ClassName()) == "TControl")
{
TControl *TheObject = (TControl *)ListBox1->Items->Objects[Index];

StatusBar1->SimpleText =
TheObject->Name + " is at (" +
IntToStr(TheObject->Left) + ", " +
IntToStr(TheObject->Top) + ")";
break;
}
if (ClassRef == NULL) // if it wasn't a control
MessageBeep(0);
}
}
Maconel 2005-01-12
  • 打赏
  • 举报
回复
Controls[i]->ClassName()可以得到他的类名,比如如果是button控件,就能得到"TButton",
Controls[i]->Name可以得到这个控件的名字,比如得到"Button1"。
samchoy 2005-01-12
  • 打赏
  • 举报
回复
如果是要找某种类型的控件,可以用ClassNameIs()


if (Controls[0]->ClassNameIs("TEdit"))
dosomething...
samchoy 2005-01-12
  • 打赏
  • 举报
回复
Controls[i]->ClassName()

ShowMessage(Controls[0]->ClassName());

604

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder VCL组件使用和开发
社区管理员
  • VCL组件使用和开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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