procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var i:integer; begin for i:=0 to ComponentCount-1 do begin if (Components[i] is Tlabel) then showmessage('1'); end; end;
我的意思是: 如我创建了三个Label for i:=1 to 3 do begin mainform.vLabel[i]:=TLabel.Create(Application); mainform.vLabel[i].Name:='Label'+inttostr(17+i); mainform.vLabel[i].Parent:=mainform.Panel1; mainform.vLabel[i].caption:=stringname;//stringname每次的字符都不一样 mainform.vLabel[i].OnMouseDown := mainform.LabellMouseDown; //我在程序运行后,在点击这三个Label中的任何一个时,我怎么知道这个Label的caption是什么呢? end;
Best_thinker() 说的应该可以 然后(Sender as TLabel).Caption
showmessage(TLabel(sender)).caption)即可
MyLabel.OnClick:=Label1.OnClick();procedure TForm1.Label1Click(Sender: TObject); begin Showmessage((sender as tlabel).Caption); end; 就可以了
unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, AppEvnts, StdCtrls, Buttons;type TForm1 = class(TForm) Label1: TLabel; Label2: TLabel; Label3: TLabel; Label4: TLabel; ApplicationEvents1: TApplicationEvents; BitBtn1: TBitBtn; procedure ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.DFM}procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean); Var P:TPoint; cControl:TControl; Begin if (Msg.message =WM_LBUTTONDOWN)Then Begin GetCursorPos(P); cControl:=FindDragTarget(P, True); if cControl.ClassName ='TLabel' Then Showmessage(cControl.Name); End; end;end.
I test it, it work ok.I can't input Chinese word.
I test it, it work ok.I can't input Chinese word.
procedure LabellMouseDown(sender :object) begin showmessage((Sender as TLabel).Caption) end;
Shift: TShiftState; X, Y: Integer);
var
i:integer;
begin
for i:=0 to ComponentCount-1 do
begin
if (Components[i] is Tlabel) then
showmessage('1');
end;
end;
如我创建了三个Label
for i:=1 to 3 do
begin
mainform.vLabel[i]:=TLabel.Create(Application);
mainform.vLabel[i].Name:='Label'+inttostr(17+i);
mainform.vLabel[i].Parent:=mainform.Panel1;
mainform.vLabel[i].caption:=stringname;//stringname每次的字符都不一样
mainform.vLabel[i].OnMouseDown := mainform.LabellMouseDown;
//我在程序运行后,在点击这三个Label中的任何一个时,我怎么知道这个Label的caption是什么呢?
end;
然后(Sender as TLabel).Caption
begin
Showmessage((sender as tlabel).Caption);
end;
就可以了
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
AppEvnts, StdCtrls, Buttons;type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
ApplicationEvents1: TApplicationEvents;
BitBtn1: TBitBtn;
procedure ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
Var
P:TPoint;
cControl:TControl;
Begin
if (Msg.message =WM_LBUTTONDOWN)Then
Begin
GetCursorPos(P);
cControl:=FindDragTarget(P, True);
if cControl.ClassName ='TLabel' Then
Showmessage(cControl.Name);
End;
end;end.
begin
showmessage((Sender as TLabel).Caption)
end;