
delphi7怎样定义控件数组并获得下标?
帮你做了个Demo:自己再添加了一些功能。
先看效果图:
{——————————华丽的分割线开始——————————}
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
scrBox1: TScrollBox;
pnl1: TPanel;
btn1: TButton;
edt2: TEdit;
lbl1: TLabel;
procedure btn1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure scrBox1Resize(Sender: TObject);
procedure scrBox1MouseWheelDown(Sender: TObject; Shift: TShiftState;
MousePos: TPoint; var Handled: Boolean);
procedure scrBox1MouseWheelUp(Sender: TObject; Shift: TShiftState;
MousePos: TPoint; var Handled: Boolean);
private
btn: TButton;
FList: TList; //定义一个TList变量用来装载Button
{ Private declarations }
procedure SetPosition;
procedure btnClick(Sender: TObject);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{调整位置}
procedure TForm1.SetPosition;
var
intLeft, intTop: Integer;
intCount: Integer;
begin
intLeft := 20;
intTop := 20;
for intCount := 0 to fList.Count - 1 do
begin
btn := FList.Items[intCount];
if intLeft + btn.Width > scrBox1.Width then
begin
intLeft := 20;
intTop := intTop + btn.Height + 20;
end;
btn.Left := intLeft;
btn.Top := intTop;
intLeft := intLeft + btn.Width + 20;
end;
end;
{----------单击按钮开始创建----------}
procedure TForm1.btn1Click(Sender: TObject);
var
intCount: Integer;
begin
if StrToIntDef(edt2.Text, 1) > 1000 then
begin
if MessageBox(Handle, PChar('输入的按钮个数太多了啦,电脑会卡死的!!!'+#10#13+'你还要继续吗?'), '提示', MB_ICONINFORMATION + MB_YESNO) = mrNo then
Exit;
end;
{如果FList已存在对象,则先释放掉}
if Assigned(FList) then
for intCount := 0 to FList.Count - 1 do
TObject(FList[intCount]).Free;
FList.Clear; //释放完后清空指针,即nil(此行代码相当重要,没有就会出错!)
{默认创建一个Button}
for intCount := 1 to StrToIntDef(edt2.Text, 1) do
begin
btn := TButton.Create(nil); //创建控件(参数为self时,不用手动释放,Form关闭后自动释放)
FList.Add(btn); //把创建的Button放到FList中
SetPosition; //调整位置
btn.Parent := scrBox1; //显示在ScrollBox上面(此行代码很关键,没有就不显示了)
btn.Caption := '按钮'+ IntToStr(intCount);
btn.OnClick := btnClick; //动态创建按钮的单击事件
end;
scrBox1.SetFocus;
end;
给分,就贴完。。。
{——————————华丽的分割线结束——————————}
PS:1.至于上面的FList变量什么作用及用法,请自己去寻找;
2.我没有用动态数组,用的TList类更灵活;
3.我做的Demo中的按钮能随窗口的改变,位置而改变,自己去试;
4.这个可以创建任意个按钮,不过输入的个数不要太多,电脑配置不好,会卡死。
5.我用的Delphi2007,只要你不是用的Delphi XE(2),其它的应该都没问题;
6.还有不懂的再问;
7.代码贴出来了,才发现没分,我郁闷,搞毛啊!提问不给分,白白辛苦,
需要完整的代码请加50分!!!
2012-07-18 · 知道合伙人软件行家

var
btn: TButton;
i:integer;
begin
for i := 0 to 10 do
begin
btn := TButton.create(self);
btn.top := 200;
btn.left := (i + 1) * 50;//间隔
btn.width := 100;
btn.tag := i;
..
..
btn.onClick := buttonClick;
end;
buttonClick(Sender:Tobject)
begin
showmessage(TButton(Sender).tag);
end;
我只是简写了代码, 有不清楚再问
btn,btn1: TButton;
i : integer;
begin
for i:= 1 to 10 do
begin
btn := TButton.Create(Self);
btn.Parent := Self;
btn.Left := btn.Width*(i-1);
btn.Caption := IntToStr(i);
btn.tag = i;
btn.OnClick := btnClick;
end;
end;
procedure TForm1.btnClick(Sender: TObject);
begin
Edit1.text := (Sender as TButton).caption;
//或者是
// Edit1.text := IntToStr((Sender as TButton).Tag);
end;