读书人

一个popupmenu和几个listview怎么共

发布时间: 2012-02-28 13:06:36 作者: rapoo

一个popupmenu和几个listview,如何共用代码
procedure TForm1.Pop1_1Click(Sender: TObject);
var a:string;
begin
a:=PopMenu1.PopupComponent.Name;
if a = 'ListView1 ' then begin
.......
exit;
end;
if a = 'ListView2 ' then begin
.......
exit;
end;
if a = 'ListView3 ' then begin
.......
exit;
end;
if a = 'ListView4 ' then begin
.......
exit;
end;


我现在的。。。有没办法共用啊

[解决办法]
procedure TForm1.Pop1_1Click(Sender: TObject);
begin
with TListView(PopupMenu1.PopupComponent) do
begin
AddItem( 'DoSomething ',nil);//这是举例应用
//.....
end;
end;

上面是用的with开域语句。如果不用,就是这样:

procedure TForm1.Pop1_1Click(Sender: TObject);
begin
TListView(PopupMenu1.PopupComponent).AddItem( 'DoSomething ',nil);
TListView(PopupMenu1.PopupComponent).Color := clRed;
//.....上面的AddItem和改变Color等应用,都是举例,你根据自己的需要去做
end;
[解决办法]
TListView(PopupMenu1.PopupComponent)的形式与 (PopupMenu1.PopupComponent as TListView)是一样的效果。
[解决办法]
当然,如果PopupMenu不仅仅与几个TListView关联,应该这样做一下判断:

procedure TForm1.Pop1_1Click(Sender: TObject);
begin
if PopupMenu.PopupComponent is TListView then //加上这个判断
with TListView(PopupMenu1.PopupComponent) do
begin
AddItem( 'DoSomething ',nil);//这是举例应用
//.....
end;
end;

读书人网 >.NET

热点推荐