473,385 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to determine the clicked column index in a Listview

I have a List view displaying data in Detail mode with several columns.

How I can get the column index the user clicked on ? (when user click on an item inside the ListView, not on a column hearder..)



Thanks for any help !



Steph.
Jan 6 '06 #1
4 10608
Hi,
Dont remember if you have this info in the listviewitem , otherwise it should be easy to calculate, in the MouseDown event you get the coordinates of the click , a simple math will detect in which column the clicked occurrd.
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Steph." <st***@nomail.com> wrote in message news:%2****************@TK2MSFTNGP11.phx.gbl...
I have a List view displaying data in Detail mode with several columns.

How I can get the column index the user clicked on ? (when user click on an item inside the ListView, not on a column hearder..)



Thanks for any help !



Steph.
Jan 6 '06 #2
Steph. wrote:
I have a List view displaying data in Detail mode with several columns.

How I can get the column index the user clicked on ? (when user click on
an item inside the ListView, not on a column hearder..)

Thanks for any help !

Steph.


You can use the sub item hittest of the listview. The code below also
takes care about column reordering.

[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("User32.dll")]
private static extern int SendMessage(IntPtr hWnd,
int msg , int wParam , ref RECT lParam);

[DllImport("User32.dll")]
private static extern int SendMessage(IntPtr hWnd,
int msg , int wParam , int[] lParam);

private bool HitTest(ListView mListView, Point hitPoint,
out int row, out int column)
{
const int LVM_GETSUBITEMRECT = 0x1038; //Is LVM_FIRST (0x1000) + 56
const int LVM_COLUMNORDERARRAY = 0x103B;//Is LVM_FIRST (0x1000)+59
const int LVIR_BOUNDS = 0;
bool retval = false;
RECT subItemRect;
row = column = -1;
ListViewItem item = mListView.GetItemAt(hitPoint.X, hitPoint.Y);

if(item != null && mListView.Columns.Count > 1)
{
if(mListView.AllowColumnReorder)
{
int[] columnOrder = new int[mListView.Columns.Count];
// Get the order of columns in case
// they've changed from the user.
if(SendMessage(mListView.Handle,
LVM_COLUMNORDERARRAY, mListView.Columns.Count,
columnOrder) != 0)
{
int i;
// Get the subitem rectangles (except column 0),
// but get them in the proper order.
RECT[] subItemRects = new RECT[mListView.Columns.Count];
for(i = 1; i < mListView.Columns.Count; i++)
{
subItemRects[columnOrder[i]].top = i;
subItemRects[columnOrder[i]].left = LVIR_BOUNDS;
SendMessage(mListView.Handle,
LVM_GETSUBITEMRECT, item.Index,
ref subItemRects[columnOrder[i]]);
}

// Find where column 0 is.
for(i = 0; i < columnOrder.Length; i++)
if(columnOrder[i] == 0)
break;

// Fix column 0 since we can't get
// the rectangle bounds of it using above.
if(i > 0)
{
// If column 0 not at index 0, set using the previous.
subItemRects[i].left = subItemRects[i-1].right;
subItemRects[i].right = subItemRects[i].left
+ mListView.Columns[0].Width;
}
else
{
// Else, column 0 is at index 0, so use the next.
subItemRects[0].left = subItemRects[1].left -
mListView.Columns[0].Width;
subItemRects[0].right = subItemRects[1].left;
}

// Go through the subitem rectangle bounds and
// see where our point is.
for(int index = 0; index < subItemRects.Length; index++)
{
if(hitPoint.X >= subItemRects[index].left &
hitPoint.X <= subItemRects[index].right)
{
row = item.Index;
column = columnOrder[index];
retval = true;
break;
}
}
}
}
// No column reordering...much simpler.
else
{
for(int index = 1; index <= mListView.Columns.Count-1;
index++)
{
subItemRect = new RECT();
subItemRect.top = index;
subItemRect.left = LVIR_BOUNDS;
if(SendMessage(mListView.Handle,
LVM_GETSUBITEMRECT, item.Index, ref subItemRect) != 0)
{
if(hitPoint.X < subItemRect.left)
{
row = item.Index;
column = 0;
retval = true;
break;
}
if(hitPoint.X >= subItemRect.left & hitPoint.X <=
subItemRect.right)
{
row = item.Index;
column = index;
retval = true;
break;
}
}
}
}
}
return retval;
}

HTH,
Andy
--
To email me directly, please remove the *NO*SPAM* parts below:
*NO*SPAM*xmen40@*NO*SPAM*gmx.net
Jan 6 '06 #3

I found a solution.... for Windows.... it works great ! :

class TListViewFFct
{

[StructLayout(LayoutKind.Sequential)] struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd,
int wMsg , int wParam , ref RECT lParam);
public static int GetListViewSubItem(ListView AListView, Point APoint)
{

const int LVM_FIRST = 0x1000;
const int LVM_GETSUBITEMRECT = (LVM_FIRST + 56);
const int LVIR_BOUNDS = 0;

RECT myrect;
ListViewItem lvitem = AListView.GetItemAt(APoint.X, APoint.Y);
if(lvitem == null && AListView.SelectedItems.Count > 0)
lvitem = AListView.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(AListView.Handle, LVM_GETSUBITEMRECT,
lvitem.Index, ref myrect);
if (APoint.X < myrect.left)
{
LVSubItem = lvitem.SubItems[0];
intLVSubItemIndex = 0;
break;
}
else if (APoint.X >= myrect.left & APoint.X <= myrect.right)
{
intLVSubItemIndex = i;
break;
}
else
LVSubItem = null;
}
}
if (LVSubItem == null || lvitem == null)
{
intLVSubItemIndex = -1;
}
return intLVSubItemIndex;
}
}//EndClass TListViewFFct

Jan 9 '06 #4
:--)) Thanks !!
Jan 9 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: VR | last post by:
Is there any to right-justify the 0th (main/item's) column in ListView? I have a ListView control, which I send a LVM_SETCOLUMNORDERARRAY message to change the order of columns. Neither before...
1
by: | last post by:
How can I know what row number I clicked on in a listview? I need a method that returns an int corresponding to the row number I clicked on in my listview. Thanks.
1
by: Steph. | last post by:
Hi, How can I get a column's position in a ListView ??? "Index" and "IndexOf" only return the position at which the column has been created, NOT the real position (after user have...
2
by: Daniel Walzenbach | last post by:
Hi, I created an ASP.NET Datagrid where a single row can be selected by clicking anywhere on the row (according to...
6
by: SStory | last post by:
Can anyone tell me what algorithm I should use to determine how many tabs to add to a string. I have two strings. I want to add them to a listbox concatenated but with a tab between them. ...
1
by: Mat | last post by:
Reordering Column changes column Indexes???? i use column.index to access(or add ) data in listview. i want to allow user to reorder columns as they need. So i am afraid to get wrong data when user...
2
by: Richard Markus | last post by:
How can I detect in code when a user has resized a column in a listview control? Thanks for the help! Richard
1
by: TheSteph | last post by:
Any way to find the Clicked SubItem in a ListView ? private int GetSubItemAtPos(ListView AListView, Point APoint) { int PositionCounter = 0; int TmpRelativeColIndex = -1; //Browse All Column...
4
by: Steve | last post by:
Hi all, I don't want to use the datagrid if I don't have to. Is there a way to setup a ListBox to have more than one checkbox column? I need something like this | Include || Set as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.