Listview Column Issues

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Listview Column Issues

Postby HsiaLin » Fri Apr 12, 2019 6:53 pm

I have an app that has 2 large ListView controls side by side. I need to be able to click on an
item and know which column was clicked so i can use the value from the clicked column.
I am attaching a tiny source to show what my issue is. I can get the column clicked as long as
the scrollbar has not been moved. This is limiting my ability to click on the last column and
get a proper result. For some reason once the scrollbar is moved, it ruins the column width
math and i have no idea why. Has anyone ever had this issue? If so what is the remedy.
Attachments
ColumnTest-LV-fixed.zip
(45.62 KiB) Downloaded 91 times
Last edited by HsiaLin on Sat Apr 13, 2019 2:32 pm, edited 2 times in total.
HsiaLin
BCBJ Master
BCBJ Master
 
Posts: 314
Joined: Sun Jul 08, 2007 6:29 pm

Re: Listview Column Issues

Postby HsiaLin » Sat Apr 13, 2019 9:43 am

Finally figured it out by adding the scroll position to X.

This will fix it:
Code: Select all
//---------------------------------------------------------------------------

void __fastcall TForm1::ListView1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
    TListItem *Item;
    int Wid;
    int Col;


    int hScroll = GetScrollPos(ListView1->Handle, SBS_HORZ);
    Item = ListView1->GetItemAt(0, Y);
    if((Item != NULL))
    {
       Wid =  0;
       Col = -1;
       do
       {
          Col++;
          if(Col == ListView1->Columns->Count - 1)
            break;
          Wid += ListView1->Column[Col]->Width;
       }
       while(!(X + hScroll < Wid));
       columnum = Col;
    }
    else
    {
       columnum = -1;
    }

    ShowMessage(L"You clicked column number " + IntToStr(columnum));

}
HsiaLin
BCBJ Master
BCBJ Master
 
Posts: 314
Joined: Sun Jul 08, 2007 6:29 pm

Re: Listview Column Issues

Postby rlebeau » Wed Apr 17, 2019 1:45 pm

HsiaLin wrote:Finally figured it out by adding the scroll position to X.


There is a much easier way - simply send the ListView's HWND a LVM_HITTEST message with wParam=-1. The return value will be the index of the item, and the output LVHITTESTINFO will contain the column index.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1597
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: Listview Column Issues

Postby HsiaLin » Wed Apr 17, 2019 9:21 pm

Ahh thanks for the info, had to use ListView_SubItemHitTest.
Seems there's always a better way.

Code: Select all
    LVHITTESTINFO htinfo = {0};
    htinfo.pt = Point(X, Y);
    ListView_SubItemHitTest(ListView1->Handle, &htinfo);
    ShowMessage(L"You clicked column number " + IntToStr(htinfo.iSubItem));
HsiaLin
BCBJ Master
BCBJ Master
 
Posts: 314
Joined: Sun Jul 08, 2007 6:29 pm

Re: Listview Column Issues

Postby rlebeau » Wed Apr 17, 2019 10:39 pm

HsiaLin wrote:had to use ListView_SubItemHitTest.


Yes, that is the one I was originally thinking of (the LVM_SUBITEMHITTEST message), I just couldn't remember the name of it (it has been a long time since I last needed to use it).
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1597
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA


Return to Technical

Who is online

Users browsing this forum: No registered users and 28 guests

cron