locked
SelectedItem in ListView - non existant? RRS feed

  • Question

  • So, I was making a feature in my program including a ListView which extends when you hover your mouse over it. Now, this works, but when you click an item, it should snap (in a richtextbox) to the selected item's Tag. (This is set when the program registers a 'separator' (that's what they're called))

    But I see there's no SelectedItem in a ListView, so I used this:
        Private Sub Separators_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Separators.SelectedIndexChanged
            Dim start As Integer
    
            Dim SelectedItems As ListView.SelectedListViewItemCollection = _
            CType(sender, ListView).SelectedItems
    
            If (SelectedItems.Count > 0) Then
                start = SelectedItems(0).Tag
    
                editBox.SelectionStart = start
                editBox.ScrollToCaret()
            End If
        End Sub

    Now, the problem is - whatever item in the ListView you click, it snaps to the latest added separator line in the richtextbox! Anyone has an alternative to what I'm doing?
    This is the code where I do stuff with separators: (posting the whole code would be too long, it's about 2000 lines just in one of 20 files)
        Private sSize As New Size(293, 19)
    
        Private Sub Separators_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Separators.MouseEnter
            If Separators.Items.Count > 0 Then
                Separators.Focus()
                CodeBlocks.Hide()
    
                Dim Times As Integer = Separators.Items.Count * 20
                If Separators.Items.Count = 1 Then
                    Times = Separators.Items.Count * 20 * 2
                End If
                Separators.Size = New Size(293, Times)
    
                If Separators.Size.Height > 600 Then
                    Separators.Size = New Size(293, 600)
                End If
    
                Separators.View = View.LargeIcon
            End If
        End Sub
    
        Private Sub Separators_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Separators.MouseLeave
            If Separators.Items.Count > 0 Then
                Separators.Size = sSize
                Separators.View = View.SmallIcon
                CodeBlocks.Show()
            End If
        End Sub
    
        Private Sub Separators_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Separators.SelectedIndexChanged
            Dim start As Integer
    
            Dim SelectedItems As ListView.SelectedListViewItemCollection = _
            CType(sender, ListView).SelectedItems
    
            If (SelectedItems.Count > 0) Then
                start = SelectedItems(0).Tag
    
                editBox.SelectionStart = start
                editBox.ScrollToCaret()
            End If
        End Sub
            If (e.KeyData = Keys.Space Or e.KeyData = Keys.Enter) And sepAdd Then
                'a separator is about to be added
                'LockWindowUpdate to prevent flickering
                LockWindowUpdate(Me.Handle.ToInt32)
    
                'first we clear all separators
                Separators.Clear()
    
                Dim sStart As Integer = editBox.SelectionStart
                Dim sLength As Integer = editBox.SelectionLength
    
                editBox.SelectionStart = sepStart
                editBox.SelectionLength = sepLength
    
                Dim sepMatches As MatchCollection = Regex.Matches(editBox.Text, "//#\s(?<name>[a-zA-Z][a-zA-Z0-9]+)")
                For Each sepMatch As Match In sepMatches
                    Dim name As String = sepMatch.Groups("name").Value
    
                    Dim start As Integer = sepStart
                    Dim blockItem As String = name
                    Dim item As New ListViewItem(blockItem)
                    With item
                        .ImageIndex = 3
                    End With
    
                    If start > -1 Then
                        item.Tag = start
                    End If
    
                    Separators.Items.Add(item)
                Next
    
                'reset stuff
                sepAdd = False
                editBox.SelectionStart = sStart
                editBox.SelectionLength = sLength
    
                'unlock the window
                LockWindowUpdate(0)
            End If

    Thanks for any help you're able to give.
    Wednesday, June 10, 2009 4:24 PM

Answers

  • Although there's no SelectedItem there is a SelectedItems collection which you use instead.

    If MultiSelect is set to False then ListView1.SelectedItems(0) corresponds to the non-existent ListView1.SelectedItem.

    • Marked as answer by Theo Storm Thursday, June 11, 2009 7:22 AM
    Wednesday, June 10, 2009 6:46 PM

All replies

  • Although there's no SelectedItem there is a SelectedItems collection which you use instead.

    If MultiSelect is set to False then ListView1.SelectedItems(0) corresponds to the non-existent ListView1.SelectedItem.

    • Marked as answer by Theo Storm Thursday, June 11, 2009 7:22 AM
    Wednesday, June 10, 2009 6:46 PM
  • Thanks Dave, but it didn't fix it =(
    It still snaps to the latest separator added's Tag and not the appropriate one.

    Code is the same as in the first post.
    Any ideas?

    Edit: fixed it!
    Thursday, June 11, 2009 6:39 AM