Click here to Skip to main content
15,911,707 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using listview in WindowApplication(C# 2.0).In Listview having 5 columns,10 Row(Dynamic).I want selected cell X,Y Postion(for example i click in 3 column of second Row)
Posted
Updated 23-Jul-10 8:52am
v2

You could use Mouse.Position to find out where the mouse is ( it's something like that, the Mouse class has a static method to tell you where the mouse is at that point in time ). If you know the position of the control, and the size of your columns and rows, you can work it out from there. I don't think a listview differentiates between the columns you click on.
 
Share this answer
 
Comments
Murugesan Solaiyappan 24-Jul-10 11:48am    
always i tried this concept.if we are not click properly on cell i can get wrong X,Y position only.
There's not a straight-forward way to do this.

Primarily because AFAIK, you can't technically select any column besides the first one since the others are all sub-items and you're really only selecting the item.

But, if you set each sub-items tag with an int representing the column associated with that sub-item, then you could easily tell where the user clicked using the MouseDown event like so:

C#
private void listView2_MouseDown(object sender, MouseEventArgs e)
{
    ListViewHitTestInfo newInfo = listView2.HitTest(e.X, e.Y);
    ListViewItem hitItem = null;
    if (newInfo.Item != null)
    {
        if (newInfo.Item.Index >= 0)
        {
            hitItem = listView2.Items[newInfo.Item.Index];
        }
    }
    if (hitItem != null)
    {
        for (int i = 0; i < (hitItem.SubItems.Count - 1); i++)
        {
            if (hitItem.SubItems[i].Tag == newInfo.SubItem.Tag)
            {
                MessageBox.Show("Row=" + (newInfo.Item.Index + 1).ToString() + 
                                Environment.NewLine + "Column=" + 
                                (i + 1).ToString());
            }
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900