在DELPHI中实现VB中类似控件数组的功能后,怎样识别同类控件的不同事件?

解决方案 »

  1.   

    把每个控件的Tag属性排好序,在控件的事件里识别Tag来完成
      

  2.   

    unit Unit1;
    {New一个Application,copy代码覆盖原代码,在对象监视器(Object Inspector)
    中通过下拉选择,设置Form1的OnCreate事件为FormCreate,然后直接运行就可看到效果}
    interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
        procedure MyClick(Sender:TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      a: array[1..100] of tbutton;implementation{$R *.dfm}procedure TForm1.MyClick(Sender: TObject);
    var
      i: integer;
      nam: string;
    begin
      i := strtoint(Trim(Copy(TButton(Sender).Name,7,3)));
      TButton(Sender).Visible := not TButton(Sender).Visible;
      i := i + 1;
      nam := 'button' + inttostr(i);
      if i <= 100 then
        TButton(FindComponent(nam)).Visible := not TButton(FindComponent(nam)).Visible
    end;procedure TForm1.FormCreate(Sender: TObject);
    var
      i: integer;
    begin
      for i := 1 to 100 do
      begin
        a[i] := tbutton.Create(self);
        with a[i] do
        begin
          Top := (i-1) div 10 * 20;
          Left := (i-1) mod 10 * 20;
          Height := 20;
          Width := 20;
          Name := 'button' + inttostr(i);
          Caption := '';
          Parent := Form1;
          OnClick := MyClick;
        end;
      end;
    end;end.