tag 帮定自定义事件后判断if sender as TSpeedButton then begin showmessage((sender as TSpeedButton).name); end;
我就想获得这个按钮的二维数组的编号,如果动态创建的时候sb[2,5],当我点击它的时候,给我显示‘2,5’就可以了,我不知道如何提取这个按钮的数组编号。 我是这样创建动态SpeedButton的:var i,j,n: Integer; DL: TSpeedButton; begin for i := 1 to 10 do begin for j := 1 to 10 do DL[i,j] := TSpeedButton.Create(Self); DL[i,j].Width := 22; DL[i,j].Height := 22; DL[i,j].Top := (j-1)*22; DL[i,j].Left := (i-1)*22; DL[i,j].Parent := form1; DL[i,j].Visible := True; end; end;
暂时没更好的,楼上方法就不错,用tag或者给TSpeedButton命名时做点手脚。
这也是个办法,但name只能存一个字符串,有点不方便,有没有更直接的办法?
var i,j,n: Integer; DL: TSpeedButton; begin for i := 1 to 10 do begin for j := 1 to 10 do DL[i,j] := TSpeedButton.Create(Self); DL[i,j].Width := 22; DL[i,j].Height := 22; DL[i,j].Top := (j-1)*22; DL[i,j].Left := (i-1)*22; DL[i,j].Parent := form1; DL[i,j].Visible := True; DL[i,j].Tag := i * 100 + j; //增加这一行 end; end;
事件处理代码里面这样写:var i, j: Integer; begin if sender as TSpeedButton then begin i := (sender as TSpeedButton).Tag div 100; j := (sender as TSpeedButton).Tag mod 100; ...... end; end;
你这个比我的看来更好,我是在Hints中增加了个字符串‘2.5’,用的时候还要分析出来。
一维编号:high(sb);二维编号:high(sb[0]); 循环写法:var i,j:integer; begin for i:=0 to high(sb) do for j:=0 to high(sb[0]) do begin //处理你要处理的事情。 end; end;
楼主怎么还在问这个问题,你的扫雷还没有完成? 楼主难道不能从数组里面查询吗? 用Tag方法也可以,以SpeedButton对象为参数在数组里查找也可以 这是我的示例代码 其中有一个普通按钮,叫CreateSpeedButtons,用来初始化SpeedButton数组 有一个自定义了函数,SpeedButtonClick,用来响应这100个按钮的点击事件。unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons;type TForm1 = class(TForm) CreateSpeedButtons: TButton; procedure CreateSpeedButtonsClick(Sender: TObject); private { Private declarations } ButtonArray:Array [1..10,1..10] of TSpeedButton; Function FindElem(aSpeedButton:TSpeedButton):TPoint; public { Public declarations } procedure SpeedButtonClick(Sender: TObject); end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.CreateSpeedButtonsClick(Sender: TObject); var i,j:Integer; begin //创建数组对象 for i:=1 to 10 do begin for j:=1 to 10 do begin ButtonArray[i,j]:=TSpeedButton.Create(self); ButtonArray[i,j] := TSpeedButton.Create(Self); ButtonArray[i,j].Width := 22; ButtonArray[i,j].Height := 22; ButtonArray[i,j].Top := (j-1)*22; ButtonArray[i,j].Left := (i-1)*22; ButtonArray[i,j].Parent := form1; ButtonArray[i,j].Visible := True; //每一个SpeedButton的OnClick事件的响应函数都指向SpeedButtonClick函数 ButtonArray[i,j].OnClick:=SpeedButtonClick; end; end;end;function TForm1.FindElem(aSpeedButton: TSpeedButton): TPoint; var i,j:Integer; begin //以按钮对象本身为参数在按钮数组ButtonArray中查找, //如果是数组中的一个值,则返回其下标 //如果未找到,则返回(-1,-1)作为下标 Result.X:=-1; Result.Y:=-1; for i:=1 to 10 do begin for j:=1 to 10 do begin if ButtonArray[i,j]=aSpeedButton then //如果参数值与数组的值匹配,则取当前的编号并返回 begin Result.X:=i; Result.Y:=j; Exit; end; end; end; end;procedure TForm1.SpeedButtonClick(Sender: TObject); var aPoint:TPoint; begin //按钮点击事件,显示数组中的序号 if Sender is TSpeedbutton then begin aPoint:=FindElem(TSpeedButton(Sender)); if ((aPoint.X<>-1) and (aPoint.y<>-1)) then //如果查找正确 begin ShowMessage(Inttostr(aPoint.X )+','+Inttostr(aPoint.Y )); end; end; end;
帮定自定义事件后判断if sender as TSpeedButton then
begin
showmessage((sender as TSpeedButton).name);
end;
我是这样创建动态SpeedButton的:var
i,j,n: Integer;
DL: TSpeedButton;
begin
for i := 1 to 10 do
begin
for j := 1 to 10 do
DL[i,j] := TSpeedButton.Create(Self);
DL[i,j].Width := 22;
DL[i,j].Height := 22;
DL[i,j].Top := (j-1)*22;
DL[i,j].Left := (i-1)*22;
DL[i,j].Parent := form1;
DL[i,j].Visible := True;
end;
end;
这也是个办法,但name只能存一个字符串,有点不方便,有没有更直接的办法?
var
i,j,n: Integer;
DL: TSpeedButton;
begin
for i := 1 to 10 do
begin
for j := 1 to 10 do
DL[i,j] := TSpeedButton.Create(Self);
DL[i,j].Width := 22;
DL[i,j].Height := 22;
DL[i,j].Top := (j-1)*22;
DL[i,j].Left := (i-1)*22;
DL[i,j].Parent := form1;
DL[i,j].Visible := True;
DL[i,j].Tag := i * 100 + j; //增加这一行
end;
end;
i, j: Integer;
begin
if sender as TSpeedButton then
begin
i := (sender as TSpeedButton).Tag div 100;
j := (sender as TSpeedButton).Tag mod 100;
......
end;
end;
循环写法:var i,j:integer;
begin
for i:=0 to high(sb) do
for j:=0 to high(sb[0]) do
begin
//处理你要处理的事情。
end;
end;
楼主难道不能从数组里面查询吗?
用Tag方法也可以,以SpeedButton对象为参数在数组里查找也可以
这是我的示例代码
其中有一个普通按钮,叫CreateSpeedButtons,用来初始化SpeedButton数组
有一个自定义了函数,SpeedButtonClick,用来响应这100个按钮的点击事件。unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;type
TForm1 = class(TForm)
CreateSpeedButtons: TButton;
procedure CreateSpeedButtonsClick(Sender: TObject);
private
{ Private declarations }
ButtonArray:Array [1..10,1..10] of TSpeedButton;
Function FindElem(aSpeedButton:TSpeedButton):TPoint;
public
{ Public declarations }
procedure SpeedButtonClick(Sender: TObject); end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.CreateSpeedButtonsClick(Sender: TObject);
var
i,j:Integer;
begin
//创建数组对象
for i:=1 to 10 do
begin
for j:=1 to 10 do
begin
ButtonArray[i,j]:=TSpeedButton.Create(self);
ButtonArray[i,j] := TSpeedButton.Create(Self);
ButtonArray[i,j].Width := 22;
ButtonArray[i,j].Height := 22;
ButtonArray[i,j].Top := (j-1)*22;
ButtonArray[i,j].Left := (i-1)*22;
ButtonArray[i,j].Parent := form1;
ButtonArray[i,j].Visible := True;
//每一个SpeedButton的OnClick事件的响应函数都指向SpeedButtonClick函数
ButtonArray[i,j].OnClick:=SpeedButtonClick;
end;
end;end;function TForm1.FindElem(aSpeedButton: TSpeedButton): TPoint;
var
i,j:Integer;
begin
//以按钮对象本身为参数在按钮数组ButtonArray中查找,
//如果是数组中的一个值,则返回其下标
//如果未找到,则返回(-1,-1)作为下标
Result.X:=-1;
Result.Y:=-1;
for i:=1 to 10 do
begin
for j:=1 to 10 do
begin
if ButtonArray[i,j]=aSpeedButton then //如果参数值与数组的值匹配,则取当前的编号并返回
begin
Result.X:=i;
Result.Y:=j;
Exit;
end;
end;
end;
end;procedure TForm1.SpeedButtonClick(Sender: TObject);
var
aPoint:TPoint;
begin
//按钮点击事件,显示数组中的序号
if Sender is TSpeedbutton then
begin
aPoint:=FindElem(TSpeedButton(Sender));
if ((aPoint.X<>-1) and (aPoint.y<>-1)) then //如果查找正确
begin
ShowMessage(Inttostr(aPoint.X )+','+Inttostr(aPoint.Y ));
end;
end;
end;
有两行ButtonArray[i,j] := TSpeedButton.Create(Self);
楼主去掉一行就可以了