coondog091800
asked on
Select subItem in TListView
I am using Delphi 5.
I have a TListview with the ViewStyle as vsReport. It has 8 columns and the number of rows varies. I need some code to get the string that is located where the user double clicks on the TListView. No mater what subitem they click on.
Thanks
Rich
I have a TListview with the ViewStyle as vsReport. It has 8 columns and the number of rows varies. I need some code to get the string that is located where the user double clicks on the TListView. No mater what subitem they click on.
Thanks
Rich
IM sorry, that is assuming you have rowselect = true
SHane
SHane
procedure TForm1.ListView1DblClick(S ender: TObject);
var
MP: TPoint;
Pt: TSmallPoint;
LI: TListItem;
begin
GetCursorPos(MP);
Pt := PointToSmallPoint(ListView 1.ScreenTo Client( MP ));
LI := ListView1.GetItemAt( pt.X, pt.Y );
if LI <> nil then
ShowMessage(LI.SubItems[1] );
end;
Shane
var
MP: TPoint;
Pt: TSmallPoint;
LI: TListItem;
begin
GetCursorPos(MP);
Pt := PointToSmallPoint(ListView
LI := ListView1.GetItemAt( pt.X, pt.Y );
if LI <> nil then
ShowMessage(LI.SubItems[1]
end;
Shane
If you want to determine the Column of the item selected, you need to get the item like I show above, and prbably manually determine which column was clicked by comparinng to column widths.
Shane
Shane
Try this
var
SelTxt: string;
procedure TForm1.ListView1MouseDown( Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
l: TListItem;
i: integer;
w: integer;
begin
l := ListView1.GetItemAt( 3, Y );
SelTxt := '';
if l <> nil then
begin
w := 0;
for i := 0 to ListView1.Columns.Count-1 do
begin
w := w + ListView1.Column[i].Width;
if w >= X then
begin
if i = 0 then
SelTxt := l.Caption
else
SelTxt := l.SubItems[i-1];
break;
end;
end;
end;
end;
procedure TForm1.ListView1DblClick(S ender: TObject);
begin
// do something with SelTxt
end;
var
SelTxt: string;
procedure TForm1.ListView1MouseDown(
Shift: TShiftState; X, Y: Integer);
var
l: TListItem;
i: integer;
w: integer;
begin
l := ListView1.GetItemAt( 3, Y );
SelTxt := '';
if l <> nil then
begin
w := 0;
for i := 0 to ListView1.Columns.Count-1 do
begin
w := w + ListView1.Column[i].Width;
if w >= X then
begin
if i = 0 then
SelTxt := l.Caption
else
SelTxt := l.SubItems[i-1];
break;
end;
end;
end;
end;
procedure TForm1.ListView1DblClick(S
begin
// do something with SelTxt
end;
mokule: you miss partially scrolled listview so X is not position in list columns but position in list window. From now I can figure out how to find value of scroll offset.
BOOL ListView_GetItemRect(
HWND hwnd,
int i,
RECT FAR *prc,
int code
);
PRC.LEFT returns negative value of offset.
l := ListView1.GetItemAt( 3, Y );
if not Assigned(l) then
exit;
SelTxt := '';
r.Left:=LVIR_BOUNDS;
ListView_GetItemRect(Listv iew1.Handl e,l.Index, r,LVIR_BOU NDS);
w:=r.left;
BOOL ListView_GetItemRect(
HWND hwnd,
int i,
RECT FAR *prc,
int code
);
PRC.LEFT returns negative value of offset.
l := ListView1.GetItemAt( 3, Y );
if not Assigned(l) then
exit;
SelTxt := '';
r.Left:=LVIR_BOUNDS;
ListView_GetItemRect(Listv
w:=r.left;
Yes. You are right. But You don't solve the problem :(
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
mokule: We want to grab points fast. We are wrong. The Easiest Way To Get Display Rectangle of List Item Is to Call
DisplayRect function of TListItem class :(
Althought both answers were right but we wrong. Therfore why you define LVM_ constants they definitions included in CommCtrl.pas (where you can find ListView_GetItemRect definition. My piece of code works well no need to send messages).
DisplayRect function of TListItem class :(
Althought both answers were right but we wrong. Therfore why you define LVM_ constants they definitions included in CommCtrl.pas (where you can find ListView_GetItemRect definition. My piece of code works well no need to send messages).
MikProg
Thanks again.
CommCtrl isn't appended to the uses automatically and I've omitted it somehow.
If You've tested Your answer You would know that it didn't work. Though You may argue that the wrong line is from my code but something has had to be done with it.
To be concrete. After scrolling this doesn't work.
l := ListView1.GetItemAt( 3, Y );
It was this what had to be changed.
Thanks again.
CommCtrl isn't appended to the uses automatically and I've omitted it somehow.
If You've tested Your answer You would know that it didn't work. Though You may argue that the wrong line is from my code but something has had to be done with it.
To be concrete. After scrolling this doesn't work.
l := ListView1.GetItemAt( 3, Y );
It was this what had to be changed.
This will give you the first column
ListView1.Selected.Caption
This will give you the additional columns:
ListView1.Selected.Subitem
WHere n is the column number
Shane