Link to home
Start Free TrialLog in
Avatar of coondog091800
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
Avatar of shaneholmes
shaneholmes


This will give you the first column

 ListView1.Selected.Caption

This will give you the additional columns:

 ListView1.Selected.Subitems[n]

WHere n is the column number

Shane
IM sorry, that is assuming you have rowselect = true

SHane
procedure TForm1.ListView1DblClick(Sender: TObject);
var
 MP: TPoint;
 Pt: TSmallPoint;
 LI: TListItem;
begin
GetCursorPos(MP);
Pt := PointToSmallPoint(ListView1.ScreenToClient( MP ));
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
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(Sender: TObject);
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(Listview1.Handle,l.Index,r,LVIR_BOUNDS);
 w:=r.left;


Yes. You are right. But You don't solve the problem :(
ASKER CERTIFIED SOLUTION
Avatar of mokule
mokule
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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).
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.