Results 1 to 2 of 2

Thread: Listview Selection

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    2

    Listview Selection

    Hi,

    I am having trouble with a command and tried everything but cant get it to work.

    I have a detailed list view which reads a text file and populates the contol. So when I select a line it populates 3x text boxes with the values. (there are 3 columns in the listview)

    But what I want to do is when a user clicks on a row it populates it with text in the text boxes (which works fine) but then when the user click on a section without any data then nothing should be in the text boxes.

    Here is my code:

    Code:
    Public Sub ListViewSelection()
    
            Dim list As ListViewItem = lvLocations.SelectedItems(0)
    
            If lvLocations.SelectedItems.Count > 0 Then
    
                If list.SubItems.Count >= 1 Then
    
                    If list.SubItems(1).Text <> "" Then
    
                        tbLineNo.Text = list.Index.ToString
                        tbID1.Text = list.Text
                        tbID2.Text = list.SubItems(1).Text
                        tbDPID.Text = list.SubItems(2).Text
    
                    ElseIf list.SubItems(1).Text = "" Then
    
                        tbLineNo.Clear()
                        tbID1.Clear()
                        tbID2.Clear()
                        tbDPID.Clear()
    
                    End If
    
                ElseIf list.SubItems.Count < 1 Then
    
                    tbLineNo.Clear()
                    tbID1.Clear()
                    tbID2.Clear()
                    tbDPID.Clear()
    
                End If
    
            ElseIf lvLocations.SelectedItems.Count <= 0 Then
    
                tbLineNo.Clear()
                tbID1.Clear()
                tbID2.Clear()
                tbDPID.Clear()
    
            End If
    
        End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Listview Selection

    Handle the MouseClick event and get the mouse position from the 'e' parameter. Call the GetItemAt method to get the item at that position. If there is no item then you know to clear the TextBoxes.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width