473,732 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10693
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.c om> wrote in message news:%2******** ********@TK2MSF TNGP11.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(La youtKind.Sequen tial)]
private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("User 32.dll")]
private static extern int SendMessage(Int Ptr hWnd,
int msg , int wParam , ref RECT lParam);

[DllImport("User 32.dll")]
private static extern int SendMessage(Int Ptr hWnd,
int msg , int wParam , int[] lParam);

private bool HitTest(ListVie w mListView, Point hitPoint,
out int row, out int column)
{
const int LVM_GETSUBITEMR ECT = 0x1038; //Is LVM_FIRST (0x1000) + 56
const int LVM_COLUMNORDER ARRAY = 0x103B;//Is LVM_FIRST (0x1000)+59
const int LVIR_BOUNDS = 0;
bool retval = false;
RECT subItemRect;
row = column = -1;
ListViewItem item = mListView.GetIt emAt(hitPoint.X , hitPoint.Y);

if(item != null && mListView.Colum ns.Count > 1)
{
if(mListView.Al lowColumnReorde r)
{
int[] columnOrder = new int[mListView.Colum ns.Count];
// Get the order of columns in case
// they've changed from the user.
if(SendMessage( mListView.Handl e,
LVM_COLUMNORDER ARRAY, mListView.Colum ns.Count,
columnOrder) != 0)
{
int i;
// Get the subitem rectangles (except column 0),
// but get them in the proper order.
RECT[] subItemRects = new RECT[mListView.Colum ns.Count];
for(i = 1; i < mListView.Colum ns.Count; i++)
{
subItemRects[columnOrder[i]].top = i;
subItemRects[columnOrder[i]].left = LVIR_BOUNDS;
SendMessage(mLi stView.Handle,
LVM_GETSUBITEMR ECT, item.Index,
ref subItemRects[columnOrder[i]]);
}

// Find where column 0 is.
for(i = 0; i < columnOrder.Len gth; 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.Colum ns[0].Width;
}
else
{
// Else, column 0 is at index 0, so use the next.
subItemRects[0].left = subItemRects[1].left -
mListView.Colum ns[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.Le ngth; index++)
{
if(hitPoint.X >= subItemRects[index].left &
hitPoint.X <= subItemRects[index].right)
{
row = item.Index;
column = columnOrder[index];
retval = true;
break;
}
}
}
}
// No column reordering...mu ch simpler.
else
{
for(int index = 1; index <= mListView.Colum ns.Count-1;
index++)
{
subItemRect = new RECT();
subItemRect.top = index;
subItemRect.lef t = LVIR_BOUNDS;
if(SendMessage( mListView.Handl e,
LVM_GETSUBITEMR ECT, item.Index, ref subItemRect) != 0)
{
if(hitPoint.X < subItemRect.lef t)
{
row = item.Index;
column = 0;
retval = true;
break;
}
if(hitPoint.X >= subItemRect.lef t & hitPoint.X <=
subItemRect.rig ht)
{
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.n et
Jan 6 '06 #3

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

class TListViewFFct
{

[StructLayout(La youtKind.Sequen tial)] struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user 32.dll")]
private static extern int SendMessage(Int Ptr hWnd,
int wMsg , int wParam , ref RECT lParam);
public static int GetListViewSubI tem(ListView AListView, Point APoint)
{

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

RECT myrect;
ListViewItem lvitem = AListView.GetIt emAt(APoint.X, APoint.Y);
if(lvitem == null && AListView.Selec tedItems.Count > 0)
lvitem = AListView.Selec tedItems[0];

int intLVSubItemInd ex = -1;
ListViewItem.Li stViewSubItem 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(ALi stView.Handle, LVM_GETSUBITEMR ECT,
lvitem.Index, ref myrect);
if (APoint.X < myrect.left)
{
LVSubItem = lvitem.SubItems[0];
intLVSubItemInd ex = 0;
break;
}
else if (APoint.X >= myrect.left & APoint.X <= myrect.right)
{
intLVSubItemInd ex = i;
break;
}
else
LVSubItem = null;
}
}
if (LVSubItem == null || lvitem == null)
{
intLVSubItemInd ex = -1;
}
return intLVSubItemInd ex;
}
}//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
2435
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 or after I send this message, I can right-justify the 0th column. Any ideas?
1
9336
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
1802
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 'manually' moved the column to another position.)
2
3180
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 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchTopQuestionsAboutASPNETDataGridServerControl.asp, Selecting Rows by Clicking Anywhere). Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, _ ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
6
1781
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. one tab makes them not line up as string A and B are variable length. String A is the shortest of the 2 and the Key.
1
3916
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 reorder columns. Can anyone confirm? Thanks you in advance. Mat
2
1902
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
2173
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 by DISPLAYED INDEX for (int ColDisplayCount = 0; ColDisplayCount < AListView.Columns.Count; ColDisplayCount++) {
4
5593
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 Default || Other columns... | asdasdasd digity-digity!
0
8944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9445
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9306
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9234
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3259
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2177
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.