如何定义指向一个控件的指针(50分)

  • 主题发起人 主题发起人 czy_crown
  • 开始时间 开始时间
C

czy_crown

Unregistered / Unconfirmed
GUEST, unregistred user!
在开发中需要定义一个指向控件的指针,然后通过指针变量进行操作,该如何使用,对于不同控件是否有不同.
例如控件 button, 定义一个指针P
建立关系后 可以用P 调用所有的button的 操作
如何做??
 
你可以定义一个 P:^TButton
 
在假设已知控件名且以Button控件为例的情况下,请尝试如下代码:
var:
pbutton1:^tbutton;
button1 :tbutton;
tempcomponent:tcomponent;
name:string;// 控件名
begin
tempcomponent:=form1.findcomponent(name);
if tempcomponent is tbutton then
button1 := Tbutton(tempcomponent);
button1.caption := '测试1';
pbutton1^ := button1;
pbutton1^.caption := '测试2';
end;
 
至于是使用指针还是控件本身,我认为区别不大,随你喜好
 
var mylabel1:^tlabel;
mybutton1:^TButton;
{$R *.dfm}


procedure TForm1.Button1Click(Sender: TObject);
begin
New(mylabel1);
mylabel1^:=tlabel.Create(self);
mylabel1^.Parent:=form1;
mylabel1^.Top:=10;
mylabel1^.Left:=10;
mylabel1^.Caption:='asdasd';
mylabel1^.Visible:=true;
New(mybutton1);
mybutton1^:=tbutton.Create(self);
mybutton1^.Parent:=Form1;
mybutton1^.Left:=10;
mybutton1^.Top:=100;
mybutton1^.Caption:='test';
mybutton1^.Visible:=true;

end;
 
恩,谢谢,qi_jianzhou简单实用,楼下的又给我不少新的思路.
[:D]
 
后退
顶部