We help IT Professionals succeed at work.
Get Started

Selecting items in a ListView with code

tomsj188
tomsj188 asked
on
324 Views
Last Modified: 2008-02-01
Hi experts
Have problem that have troubled me for sometime now.
What i'm trying to do is to select an item in a listview when an other item is selected.

Lets say i have a list with items that have one column and that column contains A letter, the items are sorted is alphabetic order, and what i wnat to do is to select the item that contains the letter N when the user selects the item that contains the letter C.

My code looks like this:
        Private Sub lvHydSerach_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvHydSerach.SelectedIndexChanged
            'Do the funky shit and mark N if C is marked
            'Debug.Write(lvHydSerach.SelectedItems.Count.ToString)
            If lvHydSerach.SelectedItems.Count > 0 Then
                RemoveHandler lvHydSerach.SelectedIndexChanged, AddressOf Me.lvHydSerach_SelectedIndexChanged
                Application.DoEvents()

                If WasCHydrSelectedInlvHydrSearch() Then
                    mIsInUpdateMode = True
                    For Each AListItem As ListViewItem In lvHydSerach.Items
                        If AListItem.Text = "N" Then
                            lvHydSerach.Items(AListItem.Index).Selected = True
                            Application.DoEvents()
                        End If
                    Next
                    mIsInUpdateMode = False
                End If

                AddHandler lvHydSerach.SelectedIndexChanged, AddressOf Me.lvHydSerach_SelectedIndexChanged
            End If
        End Sub

The function WasCHydrSelectedInlvHydrSearch, looks like this:
        Private Function WasCHydrSelectedInlvHydrSearch() As Boolean
            Static lbCWasIsSelected As Boolean

            For Each AListItem As ListViewItem In lvHydSerach.SelectedItems
                If AListItem.Text = "C" Then
                    If lbCWasIsSelected = False Then
                        lbCWasIsSelected = True
                        Return True
                    Else
                        Return False
                    End If

                End If
            Next
            lbCWasIsSelected = False
            Return False
        End Function


What happens is this, when pressing down the mouse button on item C I can see that both C and N is selected, but when I relese the button the selection for N disapears.
If I put a breakpoint on the line which starts with RemoveHandler ....... and starts to debug it works fine, i can just press F5 and continue and both C and N is selected, but when i remove the breakpoint the problem is back.

Does anyone have a sugestion??

/Tomas
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 7 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE