tags:

views:

146

answers:

1

I'm using ListView.GetItemAt(x,y) to retrieve an item from the ListView, but I'm not able to get the item when I set the FullRowSelect option to false.

I'm not sure of how to do it in .NET 1.1. Does anyone have any pointers?

Here's a code snippet:

public int GetListViewSubItem(ListView listView1, Point pt)
{
     const int LVM_FIRST  = 0x1000;
     const int LVM_GETSUBITEMRECT  = (LVM_FIRST + 56);
     const int LVIR_BOUNDS = 0;

     RECT myrect;
     ListViewItem lvitem = listView1.GetItemAt(pt.X, pt.Y);
     if(lvitem == null && listView1.SelectedItems.Count > 0)
         lvitem = listView1.SelectedItems[0];

     int intLVSubItemIndex = -1;
     ListViewItem.ListViewSubItem LVSubItem = null;

     if(lvitem != null)
     {
         int intSendMessage;
         for ( int i = 1; i <= lvitem.SubItems.Count - 1; i++)
         {
             LVSubItem = lvitem.SubItems[i];
             myrect = new RECT();
             myrect.top = i;
             myrect.left = LVIR_BOUNDS;
             intSendMessage = SendMessage(listView1.Handle, 
                                          LVM_GETSUBITEMRECT,
                                          lvitem.Index, ref myrect);
             if (pt.X < myrect.left)
             {
                 LVSubItem = lvitem.SubItems[0];
                 intLVSubItemIndex = 0;
                 break;
             }
             else if (pt.X >= myrect.left & pt.X <= myrect.right)
             {
                 intLVSubItemIndex = i;
                 break;
             }
             else
                 LVSubItem = null;
         }
    }

    if (LVSubItem == null || lvitem == null)
    {
        intLVSubItemIndex = -1;
    }

    return intLVSubItemIndex;
}

This method is supposed to show me which cell was clicked. Tt works, but if I change the fullrowselect to false, it returns a null value.

I even tried getitemat(0,e.y), but it didn't work.

whenever I select an item in the ListView, it highlighted with the color blue, so it is not possible to view what is selected. I'm trying to remove that blue highlight.

Any thoughts?

+1  A: 

Can you elaborate a bit on what you're trying to do?

Is your ListView's ViewStyle set to Details? Since you mention setting FullRowSelect to false I assume it is.

GetItemAt(x, y) will only work if the mouse was clicked on actual text that belongs to an item. So if you've clicked on the same row as an item but in a column that it doesn't have text for, it'll return null.

A workaround for this is to simply pass 0 in as the x parameter:

private void listView1_MouseUp(object sender, MouseEventArgs e)
{
    ListViewItem item = listView1.GetItemAt(0, e.Y);
    if (item == null)
    {
        MessageBox.Show("Null");
    }
    else
    {
        MessageBox.Show(item.Text);
    }
}

I can confirm that this works fine - it returns the item that lives on that row even if only the first column has text in it.

Matt Hamilton