喂,标题不要打这么大吧?
这很简单了,调用Button的Create,然后把OnClick事件赋值不就搞定?

解决方案 »

  1.   

    老弟真能唬人,不过就是一个动态创建按钮的问题吗?如上面所述,不过创建BUTTON后别忘了设定其位置及长度及高度及PARENT属性,事件的赋值用一个ACTIONLIST来做比较好。
      

  2.   

    这个问题不难,不用高手就可解决。放一个TFileListBox在你的窗体上,设置Name为FileList。 Visible为False。然后试一下下面的代码。unit frmMain;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, FileCtrl;type
      TForm1 = class(TForm)
        FileList: TFileListBox;
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        procedure ButtonsClick(Sender: TObject);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}
    procedure TForm1.FormCreate(Sender: TObject);
    var
      i:integer;
    begin
      FileList.Directory:='C:\Windows';
      for i:=0 to FileList.Items.Count-1 do
      begin
        with TButton.Create(Self) do
        begin
          Parent:=Self;
          Caption:=FileList.Items[i];
          Top:=i*40+10;
          Width:=300;
          OnClick:=ButtonsClick;
          Show;
        end;
      end;
    end;procedure TForm1.ButtonsClick(Sender: TObject);
    begin
      ShowMessage(TButton(Sender).Caption);
    end;end.
      

  3.   

    这个问题不难,不用高手就可解决。放一个TFileListBox在你的窗体上,设置Name为FileList。 Mask属性为*.txt。Visible为False。然后试一下下面的代码。unit frmMain;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, FileCtrl;type
      TForm1 = class(TForm)
        FileList: TFileListBox;
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        procedure ButtonsClick(Sender: TObject);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}
    procedure TForm1.FormCreate(Sender: TObject);
    var
      i:integer;
    begin
      FileList.Directory:='C:\Windows';
      for i:=0 to FileList.Items.Count-1 do
      begin
        with TButton.Create(Self) do
        begin
          Parent:=Self;
          Caption:=FileList.Items[i];
          Top:=i*40+10;
          Width:=300;
          OnClick:=ButtonsClick;
          Show;
        end;
      end;
    end;procedure TForm1.ButtonsClick(Sender: TObject);
    begin
      ShowMessage(TButton(Sender).Caption);
    end;end.
      

  4.   

    FindFirst()
    FindNext()
    TSearchRec
    查找指定目录文件
      

  5.   

    在原窗体上建一个commandbutton,注意在index中填上0,Name=CmdMe
    '
    dim li_txtCount as integer '你找到的*.txt的数量
    call createdynacontrols(li_txtCount)
    '''
    '
    private CreateDynaControls(iCount as integer)
    for i=1 to iCount
       load cmdMe.count
       cmdme(cmdme.count).caption=cstr(i)
    next
    exit sub
    '当你用完它们后记得用unload把它们一个个上释放掉'''
    private sub DropDynaControls()
       for i=1 to cmdMe.count
          unload cmdMe(i)
       next
    exit sub
      

  6.   

    //VB中是用控件数组实在的,我想Delphi中也有的。
    //如下是VB的源程序!
    /*在原窗体上建一个commandbutton,注意在index中填上0(Name=CmdMe)
    '
    dim li_txtCount as integer '你找到的*.txt的数量
    call createdynacontrols(li_txtCount)
    '''
    '
    private CreateDynaControls(iCount as integer)
    for i=1 to iCount
       load cmdMe.count
       cmdme(cmdme.count).caption=cstr(i)
    next
    exit sub
    '当你用完它们后记得用unload把它们一个个上释放掉'''
    private sub DropDynaControls()
       for i=1 to cmdMe.count
          unload cmdMe(i)
       next
    exit sub
    */
      

  7.   

    (1)用两个函数Findfirst()、FindNext()来解决自动检测指定目录下的文件(*.txt),当然也就能很容易得到相应的文件个数了;
    (2)如其他老兄所言,采用Create的方法来实现动态生成按钮。我建议你这样来处理。首先,在界面上放置你喜欢的一个按钮,并将它的visible属性设置为false;然后,在它的OnClick事件里写你预先设计好的代码。这里要注意的是,你必须根据Sender来判断到底是哪个按钮被按。这里的Sender指向便是那些按钮的Name。还有,在Create按钮时要将事先选的那个按钮的OnClick()
    赋值给这个动态生成的按钮的OnClick。
    Any question,just contact me by this mail:[email protected].
      

  8.   

    (1)用两个函数Findfirst()、FindNext()来解决自动检测指定目录下的文件(*.txt),当然也就能很容易得到相应的文件个数了;
    (2)如其他老兄所言,采用Create的方法来实现动态生成按钮。我建议你这样来处理。首先,在界面上放置你喜欢的一个按钮,并将它的visible属性设置为false;然后,在它的OnClick事件里写你预先设计好的代码。这里要注意的是,你必须根据Sender来判断到底是哪个按钮被按。这里的Sender指向便是那些按钮的Name。还有,在Create按钮时要将事先选的那个按钮的OnClick()
    赋值给这个动态生成的按钮的OnClick。
    Any question,just contact me by this mail address:[email protected].
      

  9.   

    我的代码:
    unit1.pas文件:unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;type
      TForm1 = class(TForm)
        Edit1: TEdit;
        procedure ButtonClick(Sender: TObject);
        procedure FormActivate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}procedure TForm1.ButtonClick(Sender: TObject);
    begin
      ShowMessage((Sender as TButton).Caption);
    end;procedure TForm1.FormActivate(Sender: TObject);
    var
      sr : TSearchRec;
      FileAttrs: Integer;
      Btn : array[0..99] of TButton;
      i : integer;
    begin
      i := 0;
      if FindFirst(Edit1.Text, FileAttrs, sr) = 0 then
      begin
        while FindNext(sr) = 0 do
        begin
          i := i + 1;
          btn[i] := Tbutton.Create (self);
          with btn[i] do
          begin
            parent := self;
            btn[i].top := 30 * i;
            btn[i].left := 20 ;
            btn[i].width := 160;
            btn[i].Tag := i;
            btn[i].OnClick :=  ButtonClick;
            btn[i].caption := sr.Name;
            btn[i].Visible := true;
          end;
        end;
        FindClose(sr);
      end;
    end;end.Form1.frm 文件:
    object Form1: TForm1
      Left = 192
      Top = 130
      Width = 450
      Height = 171
      Caption = 'Form1'
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      OldCreateOrder = False
      OnActivate = FormActivate
      PixelsPerInch = 96
      TextHeight = 13
      object Edit1: TEdit
        Left = 256
        Top = 56
        Width = 169
        Height = 21
        TabOrder = 0
        Text = 'c:\*.txt'
      end
    end
      

  10.   

    我的代码:unit1.pas文件:unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;type
      TForm1 = class(TForm)
        Edit1: TEdit;
        procedure ButtonClick(Sender: TObject);
        procedure FormActivate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}procedure TForm1.ButtonClick(Sender: TObject);
    begin
      ShowMessage((Sender as TButton).Caption);
    end;procedure TForm1.FormActivate(Sender: TObject);
    var
      sr : TSearchRec;
      FileAttrs: Integer;
      Btn : array[0..99] of TButton;
      i : integer;
    begin
      i := 0;
      if FindFirst(Edit1.Text, FileAttrs, sr) = 0 then
      begin
        while FindNext(sr) = 0 do
        begin
          i := i + 1;
          btn[i] := Tbutton.Create (self);
          with btn[i] do
          begin
            parent := self;
            btn[i].top := 30 * i;
            btn[i].left := 20 ;
            btn[i].width := 160;
            btn[i].Tag := i;
            btn[i].OnClick :=  ButtonClick;
            btn[i].caption := sr.Name;
            btn[i].Visible := true;
          end;
        end;
        FindClose(sr);
      end;
    end;end.unit1.dfm:
    object Form1: TForm1
      Left = 192
      Top = 130
      Width = 450
      Height = 171
      Caption = 'Form1'
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      OldCreateOrder = False
      OnActivate = FormActivate
      PixelsPerInch = 96
      TextHeight = 13
      object Edit1: TEdit
        Left = 256
        Top = 56
        Width = 169
        Height = 21
        TabOrder = 0
        Text = 'c:\*.txt'
      end
    endproject1.dpr:program Project1;uses
      Forms,
      Unit1 in 'Unit1.pas' {Form1};{$R *.RES}begin
      Application.Initialize;
      Application.CreateForm(TForm1, Form1);
      Application.Run;
    end.
      

  11.   

    (1)用两个函数Findfirst()、FindNext()来解决自动检测指定目录下的文件(*.txt),当然也就能很容易得到相应的文件个数了;
    (2)如其他老兄所言,采用Create的方法来实现动态生成按钮。我建议你这样来处理。首先,在界面上放置你喜欢的一个按钮,并将它的visible属性设置为false;然后,在它的OnClick事件里写你预先设计好的代码。这里要注意的是,你必须根据Sender来判断到底是哪个按钮被按。这里的Sender指向便是那些按钮的Name。还有,在Create按钮时要将事先选的那个按钮的OnClick()
    赋值给这个动态生成的按钮的OnClick。
    Any question,just contact me by this mail:[email protected].