[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Listview selected items

Posted on 2014-03-19
6
Medium Priority
?
465 Views
Last Modified: 2014-03-22
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:

        MsgBox(ListView1.SelectedItems.Item(0).Text)

Thanks in advance!

I am using VB.NET not C++ or C#.
0
Comment
Question by:russell12
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 36

Accepted Solution

by:
Kimputer earned 2000 total points
ID: 39941293
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
        MsgBox(val)
    End Sub

Open in new window

0
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 39941656
Your problem is here:

MsgBox(ListView1.SelectedItems.Item(0).Text)

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.
0
 
LVL 2

Author Comment

by:russell12
ID: 39942730
Kimputer:
I have not tried that solution yet.  I am going to try it tonight when I get home.  Thanks for the quick response.

Luis:
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Expert Comment

by:Luis Pérez
ID: 39942754
But that's not possible with your code:

MsgBox(ListView1.SelectedItems.Item(0).Text)

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.
0
 
LVL 2

Author Comment

by:russell12
ID: 39942873
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.
0
 
LVL 2

Author Closing Comment

by:russell12
ID: 39947904
Thanks for the quick response and help!!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question