如何为控件数组指定事件处理过程
编号:QA000832
建立日期: 1999年4月19日 最后修改日期:1999年4月19日
所属类别:
jing:
Delphl 4
win95
procedure TForm1.FormCreate(Sender: TObject);
var counter:integer;
begin
for counter := 1 to 10 do
begin
arrShape[counter]:= TShape.Create(Self);
arrShape[counter].top := Counter * 10;
arrShape[counter].Left := 20;
arrShape[counter].Height := 8;
arrShape[counter].Parent := Form1;
end;
end;
上面是我建立一个按钮数组。但不知如何编写它的响应事件的程序?请指教!并给出例子。谢谢!
回答:
Delphi一个非常Cool的特性就是它可以在程序中为控件指派事件处理过程。
先定义:
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
arrShape: array[1..10] of TShape;
public
procedure ShapeMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
end;
然后在FormCreate事件中加一句:
...
arrShape[counter].Parent := Form1;
arrShape[counter].OnMouseDown := ShapeMouseDown;
end;
而ShapeMouseDown事件如下定义:
procedure TForm1.ShapeMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
ShowMessage('你选择了第' + IntToStr(TShape(Sender).top div 10)
+ '个控件!');
end;
此问题由李海回答。
| |
|
|
| |
|
|