Solved

Listview vb.net

Posted on 2011-09-13
6
225 Views
Last Modified: 2012-05-12
Hi all,

I have a stupid question that i just cant seem to find the answer to. I use the following code to populate a  list view in vb.net.

What i cant seem to get is i want to double click on a item and only get that value

eg if my list view has 5 coloums and 5 rows i want to be able to get the data from row 5 coloum 4

I hope this makes sense
Me.ListView1.Items.Clear()
        Dim LVI As ListViewItem = Nothing
        Dim cmd As New MySqlCommand
        Dim connect As New MySqlConnection(My.Settings.dnote_dbConnectionString)
Then cmd.CommandText = "SELECT * FROM data"

        cmd.Connection = connect
        connect.Open()
        Dim read As MySqlDataReader
        read = cmd.ExecuteReader
        While read.Read()
            LVI = New ListViewItem(read.GetString(0))
            LVI.SubItems.Add(read.GetString(1))
            LVI.SubItems.Add(read.GetString(2))
            LVI.SubItems.Add(read.GetString(3))
            LVI.SubItems.Add(read.GetString(4))
            LVI.SubItems.Add(read.GetString(5))
            LVI.SubItems.Add(read.GetString(6))
            LVI.SubItems.Add(read.GetString(7))
            ListView1.Items.Add(LVI)
        End While
        read.Close()
        connect.Close()

Open in new window

0
Comment
Question by:frankie_jnr
6 Comments
 
LVL 40
ID: 36529572
React to the SelectedIndexChanged event of the ListView:
Private Sub ListView1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListView1.SelectedIndexChanged
   Debug.WriteLine(ListView1.SelectedItems(0).Text)
End Sub

Open in new window

Note that with a multiple selection, this gives you only the first item. You might want to go further to process multiple selections.
0
 

Author Comment

by:frankie_jnr
ID: 36530868
I did that but it is just not selecting anything or writing anything. Is there some property on the listview that i could have forgotten
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36532114
Do you mean you want to double on a specific row and specific column?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 15

Expert Comment

by:x77
ID: 36533103
Use ListView.HitText Method.

It Returns as ListViewHitTestInfo instance with ListItem and SubItem.

http://msdn.microsoft.com/en-us/library/system.windows.forms.listviewhittestinfo.aspx
Private Sub lv_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lv.MouseClick
    Dim info = lv.HitTest(e.X, e.Y)
End Sub

Open in new window

0
 
LVL 15

Accepted Solution

by:
x77 earned 500 total points
ID: 36533127
For DoubleClick:

    Private Sub lv_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lv.MouseDoubleClick
        Dim info = lv.HitTest(e.X, e.Y)
        If info.SubItem IsNot Nothing Then Debug.Print(info.SubItem.Text)
    End Sub
0
 

Author Closing Comment

by:frankie_jnr
ID: 36534202
Thank you very much! Works like a dream!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

919 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now