公布一段动态生成按钮button的代码
我自己做了个简单的例子,这个问题在网上很多人问,可能对好多人有用,QQ游戏的选房间就类似这个。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, DB, ADODB;
type
TForm1 = class(TForm)
ScrollBox1: TScrollBox;
ScrollBox2: TScrollBox;
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
ADOQuery2: TADOQuery;
ADOQuery1lb_dm: TStringField;
ADOQuery1lb_mc: TStringField;
ADOQuery1cd_zxh: TIntegerField;
ADOQuery2id: TAutoIncField;
ADOQuery2cd_lb: TStringField;
ADOQuery2cd_dm: TStringField;
ADOQuery2cd_mc: TStringField;
ADOQuery2cd_zxh: TIntegerField;
ADOQuery2cd_cxh: TIntegerField;
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
procedure lbttclick(Sender: TObject);
procedure bbttclick(Sender: TObject);
procedure FormActivate(Sender: TObject);
private
lbtt: array[1..20] of Tbitbtn;
bbtt: array[1..20, 1..100] of Tbitbtn;
cdbh: array[1..20, 1..100] of string;
cdlb: array[1..20, 1..100] of string;
rrow, ccol: integer;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.lbttclick(Sender: TObject); //各主按鈕的click
var i, j, k: integer;
begin
if activecontrol.ClassType <> Tbitbtn then exit;
if copy(activecontrol.Name, 1, 3) <> 'sp_' then exit;
i := pos('_', activecontrol.Name);
j := strtoint(copy(activecontrol.Name, i + 1, 2));
for k := 1 to 20 do
for i := 1 to 100 do
begin
if (bbtt[k, i].Caption <> '') and (k = j) then
begin
bbtt[k, i].Visible := true;
bbtt[k, i].BringToFront;
end
else bbtt[k, i].Visible := false;
end;
end;
procedure TForm1.bbttclick(Sender: TObject); //明細按鈕的click
var
i:integer; st:string;
begin
if activecontrol.ClassType = Tbitbtn then
if copy(activecontrol.Name, 1, 3) <> 'sp_' then exit;
st := trim(copy(activecontrol.Name, 4, 6));
i := pos('_', st);
rrow := strtoint(copy(st, 1, i - 1));
ccol := strtoint(copy(st, i + 1, 2));
listBox1.Items.Add(cdbh[rrow,ccol]+'---'+bbtt[rrow,ccol].Caption );
end;
procedure TForm1.FormCreate(Sender: TObject); //初始化button數組
var i, j, k: integer;
begin
for i := 0 to 19 do
begin
lbtt[i + 1] := Tbitbtn.Create(Self);
lbtt[i + 1].Height := 30;
lbtt[i + 1].Width := 117;
j := trunc(i / 4);
lbtt[i + 1].Top := 1 + (lbtt[i + 1].Height + 1) * j;
j := i mod 4;
lbtt[i + 1].Left := lbtt[i + 1].Width * (j);
lbtt[i + 1].Name := 'sp_' + inttostr(i + 1);
lbtt[i + 1].Parent := scrollbox1;
lbtt[i + 1].Caption := '';
lbtt[i + 1].Font.name:='新細明體';
lbtt[i + 1].Font.size:=14;
lbtt[i + 1].OnClick := lbttClick; //這裡給它寫事件
// lbtt[i + 1].Visible := false;
for j := 0 to 99 do
begin
bbtt[i + 1, j + 1] := Tbitbtn.Create(Self);
bbtt[i + 1, j + 1].Height := 33;
bbtt[i + 1, j + 1].Width := 117;
k := trunc(j / 4);
bbtt[i + 1, j + 1].Top := 1 + (bbtt[i + 1, j + 1].Height + 1) * k;
k := j mod 4;
bbtt[i + 1, j + 1].Left := bbtt[i + 1, j + 1].Width * (k);
bbtt[i + 1, j + 1].Name := 'sp_' + inttostr(i + 1) + '_' + inttostr(j + 1);
bbtt[i + 1, j + 1].Parent := scrollbox2;
bbtt[i + 1, j + 1].Caption := '';
bbtt[i + 1, j + 1].Font.name:='新細明體';
bbtt[i + 1, j + 1].Font.size:=14;
bbtt[i + 1, j + 1].OnClick := bbttclick;//這理給它寫事件
// bbtt[i + 1, j + 1].Visible := false;
end;
end;
end;
procedure TForm1.FormActivate(Sender: TObject); //初始化按鈕狀態
var i, j, k: integer;
begin
for i := 1 to 20 do
begin
lbtt[i].Caption := '';
lbtt[i].Glyph := nil;
lbtt[i].Visible := false;
for j := 1 to 100 do
begin
bbtt[i, j].Caption := '';
bbtt[i, j].Glyph := nil;
end;
end;
Adoquery1.Open;
while not Adoquery1.Eof do
begin
lbtt[ADOQuery1cd_zxh.AsInteger].Visible := true;
lbtt[ADOQuery1cd_zxh.AsInteger].Caption := ADOQuery1.FieldByname('lb_mc').AsString;
lbtt[ADOQuery1cd_zxh.AsInteger].BringToFront;
Adoquery1.Next;
end;
Adoquery1.close;
Adoquery2.Open;
Adoquery2.First;
while not Adoquery2.Eof do
begin
bbtt[ADOQuery2cd_zxh.AsInteger, ADOQuery2cd_cxh.AsInteger].Caption := ADOQuery2cd_mc.AsString;
cdlb[ADOQuery2cd_zxh.AsInteger, ADOQuery2cd_cxh.AsInteger] := ADOQuery2cd_lb.AsString;
cdbh[ADOQuery2cd_zxh.AsInteger, ADOQuery2cd_cxh.AsInteger] := ADOQuery2cd_dm.AsString;
Adoquery2.Next;
end;
Adoquery2.Close;
j := 1;
for k := 1 to 20 do
for i := 1 to 100 do
begin
if (bbtt[k, i].Caption <> '') and (k = j) then
begin
bbtt[k, i].Visible := true;
bbtt[k, i].BringToFront;
end
else bbtt[k, i].Visible := false;
end;
end;
end.
问题点数:20、回复次数:7Top
1 楼xixuemao(钱不是问题,问题是没钱)回复于 2005-09-02 20:07:20 得分 5
楼主好人啊。^_^Top
2 楼youngmo1(小莫)回复于 2005-09-02 20:41:59 得分 2
楼主,好像没有界面呀,不知道什么东西?Top
3 楼budded(All By Myself)回复于 2005-09-02 22:29:48 得分 1
看起来像[混乱之治]啊,佩服搂主的精神……Top
4 楼quicksand201(流沙)回复于 2005-09-02 23:11:17 得分 5
好玩Top
5 楼ly_liuyang(Liu Yang LYSoft http://lysoft.7u7.net)回复于 2005-09-03 00:23:24 得分 2
hehe:0Top
6 楼qybao(阿宝)回复于 2005-09-03 00:40:47 得分 5
大概看了一下
性能上也许还有待改善Top
7 楼postfxj(探索者)回复于 2005-09-07 13:46:26 得分 0
頂Top




