i是个动态的变量,Button1Click被触发,arBitBtn也就被创建。
i值也到了末尾5。上次有人告诉我arBitBtn[i]其实只是个指针而已。我想使arBitBtn[i].OnClick事件能显示类似VB的[不同的]索引值i,而不是显示5.
比如arBitBtn[2].OnClick事件能showmessage显示2
而arBitBtn[3].OnClickshowmessage显示3
//==========源代码如下=============
unit Unit1;interfaceuses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls, Buttons, ExtCtrls;type
    TForm1 = class(TForm)
        BitBtn1: TBitBtn;
        Button1: TButton;
        Image1: TImage;
        procedure Button1Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    private
        { Private declarations }
        arBitBtn: array[0..5] of TBitBtn;
    public
        { Public declarations }
    end;var
    Form1           : TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
    i:integer;
begin
    BitBtn1.Glyph.LoadFromFile('d:\winnt\Soap Bubbles.bmp');
//    BitBtn1.Glyph.Assign(Image1.Picture.Bitmap);
//    BitBtn1.Glyph := Image1.Picture.Bitmap;    for i:=0 to 5 do
    begin
        arBitBtn[i]:=TBitBtn.Create(self);
        arBitBtn[i].Glyph.LoadFromFile('d:\winnt\Soap Bubbles.bmp');
        arBitBtn[i].Left:=i*80;
        arBitBtn[i].Parent:=self;
        arBitBtn[i].OnClick:=BitBtn1Click;
//我想使arBitBtn[i].OnClick事件能显示类似VB的[不同的]索引值i,而不是显示5.
//比如arBitBtn[2].OnClick事件能showmessage显示2而arBitBtn[3].OnClickshowmessage显示3
    end;
end;procedure TForm1.BitBtn1Click(Sender: TObject);
begin
    ShowMessage('KKKKKKKKKK');
end;end.

解决方案 »

  1.   


    procedure TForm1.Button1Click(Sender: TObject);
    var
        i:integer;
    begin
        BitBtn1.Glyph.LoadFromFile('d:\winnt\Soap Bubbles.bmp');
    //    BitBtn1.Glyph.Assign(Image1.Picture.Bitmap);
    //    BitBtn1.Glyph := Image1.Picture.Bitmap;    for i:=0 to 5 do
        begin
            arBitBtn[i]:=TBitBtn.Create(self);
            arBitBtn[i].Glyph.LoadFromFile('d:\winnt\Soap Bubbles.bmp');
            arBitBtn[i].Left:=i*80;
            arBitBtn[i].tag:=i;
            arBitBtn[i].Parent:=self;
            arBitBtn[i].OnClick:=BitBtn1Click;
    //我想使arBitBtn[i].OnClick事件能显示类似VB的[不同的]索引值i,而不是显示5.
    //比如arBitBtn[2].OnClick事件能showmessage显示2而arBitBtn[3].OnClickshowmessage显示3
        end;
    end;
    procedure TForm1.BitBtn1Click(Sender: TObject);
    begin
        ShowMessage('KKKKKKKKKK'+inttostr(TBitBtn(Sender).tag));
    end;
      

  2.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
        i:integer;
    begin
        BitBtn1.Glyph.LoadFromFile('d:\winnt\Soap Bubbles.bmp');
    //    BitBtn1.Glyph.Assign(Image1.Picture.Bitmap);
    //    BitBtn1.Glyph := Image1.Picture.Bitmap;    for i:=0 to 5 do
        begin
            arBitBtn[i]:=TBitBtn.Create(self);
            arBitBtn[i].Glyph.LoadFromFile('d:\winnt\Soap Bubbles.bmp');
            arBitBtn[i].Left:=i*80;
            arBitBtn[i].Parent:=self;
            arBitBtn[I].Tag := I;//用它标识一下。
            arBitBtn[i].OnClick:=BitBtn1Click;
    //我想使arBitBtn[i].OnClick事件能显示类似VB的[不同的]索引值i,而不是显示5.
    //比如arBitBtn[2].OnClick事件能showmessage显示2而arBitBtn[3].OnClickshowmessage显示3
        end;
    end;procedure TForm1.BitBtn1Click(Sender: TObject);
    begin
      ShowMessagePro(IntToStr(Sender.Tag))
    end;