一直不会用tag 请教各位了(50分)

  • 主题发起人 qq66533885
  • 开始时间
Q

qq66533885

Unregistered / Unconfirmed
GUEST, unregistred user!
2个button,1个PopupMenu1
在button上右击使用PopupMenu1,在PopupMenu1事件里如何判断是点击了哪个button而执行相应的功能.
 
PopupMenu.PopupComponent
 
希望具体些,谢谢
 
按钮的单击时间会传入一个参数,此参数能判断出到底是单击的哪一个按钮,可以将一个按钮的tag设置为1,另外一个设置为2,接可以很容易的判断到底单击的是哪个按钮了
if (sender as TButton).tag=1 then
.....
else
.....
 
tag属性对控件本身并没有影响的,这只是提供给开发者做你自定义标识用的,事件的Sender参数就是对象本身,当然如果你是用代码调用事件过程的话就要看你传的是什么参数了,一般可以像楼上这样判断
if (Sender as TButton).Tag = 1 then
 
ShowMessage((PopupMenu1.PopupComponent).Name);
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
PopupMenu1: TPopupMenu;
a1: TMenuItem;
procedure a1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.a1Click(Sender: TObject);
begin
if (sender as TButton).tag=1 then
showmessage('1');
if (sender as TButton).tag=2 then
showmessage('2');
end;
end.
为什么会编译不过??
ShowMessage((PopupMenu1.PopupComponent).Name);
这个通过判断name我学会了,等我学会了tag再分配分数.
虽然分少,但希望知道的教教我.
 
ShowMessage(IntToStr(((PopupMenu1.PopupComponent) as TButton).Tag));
 
我终于明白了,谢谢liyinwei的耐心解说.
 
顶部