CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Delphi >  VCL组件开发及应用

公布一段动态生成按钮button的代码

楼主postfxj(探索者)2005-09-02 18:25:10 在 Delphi / VCL组件开发及应用 提问

我自己做了个简单的例子,这个问题在网上很多人问,可能对好多人有用,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

相关问题

  • 如果用代码生成一个按钮?
  • 代码生成
  • [框架窗体代码全部在内]为何无法生成一个按钮???
  • 如何用代码生成的一个服务器端的按钮,并用这个生成的按钮去执行它的Click?
  • 在客户端用代码生成的命令按钮为什么无法响应OnClick事件?
  • 我想用代码生成一个按钮,但我不太清楚怎么写,请各位帮帮忙。
  • 求生成的IE窗口不含地址栏和标准按钮的ASP代码
  • ASP生成一个按钮,其OnClick后面的代码为什么会出错?谢谢!在线等。
  • TO: taidy() ,我用你的办法动态生成了几个按钮,但是我怎样写代码响应这个按钮的Onclick事件呢
  • 用Rose2000生成C++代码...

关键词

  • lbtt
  • bbtt
  • adoquery
  • tstringfield
  • activecontrol
  • ccol
  • tbitbtn
  • rrow
  • lbttclick
  • tintegerfield

得分解答快速导航

  • 帖主:postfxj
  • xixuemao
  • youngmo1
  • budded
  • quicksand201
  • ly_liuyang
  • qybao

相关链接

  • Delphi类图书
  • Delphi类源码下载
  • Delphi控件下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo