问题未解决:自动生成的按钮,如何指定按钮的click ( 积分: 50 )

  • 主题发起人 主题发起人 丁堡
  • 开始时间 开始时间

丁堡

Unregistered / Unconfirmed
GUEST, unregistred user!
5个自动生成的按钮,name分别是button10-button15.
我想在用鼠标点击一个按钮如:button10后,用label1.capton:=button10,
然后在timer中隔10秒钟自动点击一次这个按钮,也就是实现刷新.
谢谢大家
原贴:http://www.delphibbs.com/delphibbs/dispq.asp?lid=3855620
采用lovedesky的办法
(2)实现方法:
1.timer1的属性Interval:=10000;
2.······
private
{ Private declarations }
i:integer;
······
procedure TForm1.Button10Click(Sender: TObject);
begin
label1.capton:=button10.caption;
i:=10;
end;
procedure TForm1.Button11Click(Sender: TObject);
begin
label1.Caption:=button11.Caption;
i:=11;
end;
······
procedure TForm1.Timer1Timer(Sender: TObject);
begin
case i of
10:button10.Click;
11:button11.Click;
12:button12.Click;
13:button13.Click;
14:button14.Click;
end;
end;
······
编译时提示没有此控件,因为是自动生成的,所以没有.所以提示出错
继续开贴请求解决办法
 
把所有动态生成的Button 的OnClick事件指向Button10Click。

.........................
private
ActionButton:Tbutton;
.........................
implementation


procedure TForm1.Button10Click(Sender: TObject);
begin
if Sender is Tbutton then
begin
ActionButton:= Tbutton(Sender);
label1.Caption := ActionButton.Caption;
end;

end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
if Assigned(ActionButton ) then ActionButton.Click;
end;
 
其实跟楼上的思路都差不多

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;

type
TForm1 = class(TForm)
Timer1: TTimer;
Label1: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Timer1Timer(Sender: TObject);
private
FIndex: Integer;
FButtonList: array[0..4] of TButton;
procedure ButtonOnClick(Sender: TObject);
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ButtonOnClick(Sender: TObject);
begin
FIndex := TButton(Sender).Tag;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
begin
for i := 0 to 4 do begin
FButtonList := TButton.Create(self);
FButtonList.SetBounds(80 * i,10,70,20);
FButtonList.OnClick := ButtonOnClick;
FButtonList.Caption := 'Button' + IntToStr(i);
FButtonList.Tag := i;
FButtonList.Parent := self;
end;
FIndex := -1;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
i: Integer;
begin
for i := 0 to 4 do
FButtonList.Free;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
Label1.Caption := Label1.Caption + IntToStr(FIndex);
end;
 
谢谢楼上两位指导.经过临时测试,基本实现我需要的功能了.
今晚再在程序上测试,通过后再来给分.
 
问题已解决。分配积分
 

Similar threads

后退
顶部