一个关于指针定义的基础问题

sxqinge 2012-08-17 04:18:06
以前多次看到帖子里有这样的回复,没怎么引起重视也没做相关笔记,现在用起来的时候居然发现不知道搜索关键字了:

比如有个指针TEdit *edtData3,即指针名称后面跟的是个数字,现在有个需求就是我知道后面的这个数字,如何获取这个控件的指针呢?其他控件是不是也是这么一个格式来根据指针名称后面的数字来获取指针,比如TImage控件,TShape控件等。
...全文
88 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sxqinge 2012-08-17
  • 打赏
  • 举报
回复
谢谢你们的回复,呵呵就是这样弄的,太感谢咯!
缘中人 2012-08-17
  • 打赏
  • 举报
回复
TButton   *button   =   NULL; 
button = (TButton*)FindComponent( "Button3 ");
if(button==NULL)
ShowMessage( "该控件不存在 ");


http://topic.csdn.net/t/20010903/11/268937.html
缘中人 2012-08-17
  • 打赏
  • 举报
回复
TButton   *button   =   NULL; 
button = (TButton*)FindComponent( "Button3 ");
if(button==NULL)
ShowMessage( "该控件不存在 ");

http://topic.csdn.net/t/20010903/11/268937.html
Jim@luckeeinc.com 2012-08-17
  • 打赏
  • 举报
回复
String str = "edtData3"; // 已知的名字
TEdit *edit = (TEdit *)(FindComponent(str));
缘中人 2012-08-17
  • 打赏
  • 举报
回复
名字前缀是不是固定的,如果固定,可以FindComponent
FindComponent("edt"+String(i));
看cb的官方例子

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];
}
sxqinge 2012-08-17
  • 打赏
  • 举报
回复
当时记得妖哥说过一个方法,无需数组保存,好像有个强制转换TObject*什么的,只需要知道指针后面的那个数字和指针类型,就可以得知具体的指针,实现方法记不清了。。。
dataxdata 2012-08-17
  • 打赏
  • 举报
回复
这个得事先用个数组把指针保存起来吧?然后用数字当下标来访问这个指针数组

13,825

社区成员

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

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