delphi 如何确定选中了ListView1中的某一项?ListView1.Selected.Caption???还是用Item.Index??
3个回答
展开全部
{给你写了个例子,新建一个工程,然后上面放一个ListView和一个按钮。判断是否选中用ListView1.SelCount 、 判断是否选中某一节点可用ListView1.Selected.Caption = '5' 或者ListView1.ItemIndex = 7 都可以,下面例子点5或者点7都是通过判断得到的。}
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls;
type
TForm1 = class(TForm)
ListView1: TListView;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if ListView1.SelCount > 0 then //如果选中节点
begin
if (ListView1.Selected.Caption = '5') //通过Caption判断是否选中某一节点
or (ListView1.ItemIndex = 7) then //通过ItemIndex判断是否选中某一节点
ShowMessage('test')
else
ShowMessage(ListView1.Selected.Caption); //取选中节点的Caption
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
aItem: TListItem;
i: Integer;
begin
ListView1.Items.Clear;
for i:= 0 to 10 do
begin
aItem:= ListView1.Items.Add; //添加Item
aItem.Caption:= IntToStr(i); //设置新增Item的Caption
end;
aItem.Free;
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls;
type
TForm1 = class(TForm)
ListView1: TListView;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if ListView1.SelCount > 0 then //如果选中节点
begin
if (ListView1.Selected.Caption = '5') //通过Caption判断是否选中某一节点
or (ListView1.ItemIndex = 7) then //通过ItemIndex判断是否选中某一节点
ShowMessage('test')
else
ShowMessage(ListView1.Selected.Caption); //取选中节点的Caption
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
aItem: TListItem;
i: Integer;
begin
ListView1.Items.Clear;
for i:= 0 to 10 do
begin
aItem:= ListView1.Items.Add; //添加Item
aItem.Caption:= IntToStr(i); //设置新增Item的Caption
end;
aItem.Free;
end;
end.
展开全部
ListView1.Selected.Caption 获得的是选中项的name
ListView1.ItemIndex 获得的是选中项的索引,也就是第几个。
ListView1.ItemIndex 获得的是选中项的索引,也就是第几个。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
测试一下哇
ListView1.Click()事件
caption:='正在浏览☆☆☆'+(ListView1.Selected.Caption)+'☆☆☆';
ListView1.Click()事件
caption:='正在浏览☆☆☆'+(ListView1.Selected.Caption)+'☆☆☆';
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询