Listview selected items

I have a listview that has multiple columns in it.  I have the code to show the selected item, but the problem is if I select column 2 it shows column 2's item, if I select column 1, it shows columns 1's item.  Example:

Column1 | Column2
1                   4
2                   5
3                   6

If I click on the first row under Column1, it will show "1".  If I click on the first row under Column2, it will show "4".  I need it to show "1" no matter where I click on row 1.  I hope I this isn't to confusing.  Any help would be greatly appreciated.  
The code I am working with is:


Thanks in advance!

I am using VB.NET not C++ or C#.
Who is Participating?
KimputerConnect With a Mentor Commented:
on load:

ListView1.FullRowSelect = True

Open in new window

and then
    Private Sub ListView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseUp
        Dim hti As ListViewHitTestInfo = ListView2.HitTest(e.Location)
        Dim val As String = hti.Item.SubItems(0).Text
    End Sub

Open in new window

Luis PérezSoftware Architect in .NetCommented:
Your problem is here:


You're showing a MsgBox with the Text of the first selected (SelectedItems.Item(0)) item in the ListView. So you have 2 issues:
You're showing always the text of the first selected item (that is not a problem if the MultiSelect property of your ListView is set to False).
You're showing always the text in the first column (Text property). It seems that is what you want to do, so this is right.

Hope that helps.
russell12Author Commented:
I have not tried that solution yet.  I am going to try it tonight when I get home.  Thanks for the quick response.

The listview property for multi-select is set to false.  The text in the first column is only displayed if I click the row in the first column, but if I click the row on the second column, the text from the second column is displayed.  I have tried it multiple time with same results.  You are correct, I want it to show column 1 text only no matter when I click on the row.
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Luis PérezSoftware Architect in .NetCommented:
But that's not possible with your code:


You're showing the .Text property of the item in 0 position of SelectedItems collection. So if you're showing the .Text property, it's the text in the first column.
russell12Author Commented:
I understand what you are saying trust me, thats why I am here.  I am using VB2012. The code I put is a copy and paste from my program to the web.
russell12Author Commented:
Thanks for the quick response and help!!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.