Set Listview to unselected...

I want to unselect my listview item after a click event, I know for a listbox I use listbox1.selectedIndex = -1, so how do I do this with the listview?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


To clear all items you can do this:


To clear a single item:

    Me.ListView1.Items(YourSelectedIndex).Checked = False

Hope this helps.


Wayne Taylor (webtubbs)Commented:
De-selecting a single item in a listview is done like this....

    ListView1.SelectedItems(0).Selected = False


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Hawkvalley1Author Commented:
Ok webtubbs, I just need to get the currently selected index then pass it into a variable then I can deselect it, this works great...
I was getting close thanks for sending down the right path.
Dim pack As ListView.SelectedIndexCollection = ltvUnits.SelectedIndices
        Dim index As Integer
        For Each index In pack
            ltvUnits.SelectedItems(index).Selected = False

Open in new window

CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Hawkvalley1Author Commented:
I just had to add to the answer, as there was a condition webtubbs didn't know about, but his code was the right format.
Wayne Taylor (webtubbs)Commented:
>>I just had to add to the answer, as there was a condition webtubbs didn't know about, but his code was the right format.

What condition was that?
Hawkvalley1Author Commented:
Well the (0) was not the right index, and it changes so I need to find the currently selected index and your code did not do that. It was not a slant against you - it just explained the score in the accepted solution - sorry if that bothered you, but the code wasn't complete or correct (in fact) it's current format throws errors. I guess I'll stop writing comments in there, I didn't mean to upset you, and I appreciate your help. Thanks for the continued help.
Wayne Taylor (webtubbs)Commented:
I'm not upset, just confused as to why you didn't say there was a problem before you closed the question.

With little to go on, what I posted was the best I could do. that said, your code de-selects every item, which is what kdwood's suggestion does. To de-select only the clicked item, you'd need to handle the MouseUp event, and use a hitTest to get the actual clicked item....

    Private Sub ListView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseUp
        Dim ht As ListViewHitTestInfo = ListView1.HitTest(e.Location)
        If ht.Item IsNot Nothing Then
            ht.Item.Selected = False
        End If
    End Sub

Hawkvalley1Author Commented:
Wayne, there was no problem, the code worked fine, and knew by looking at your answer how to fix it, my statement merely letting you know since I didn't give you alot of info that it my job to either give you more info or was the answer enough - and it was - problem solved.

Not sure about the mouseup event, I just need to have whatever the selected item was to unselect when I click on my button. Is there a problem with the code I used above - cause it works? I don't see how the mouseup event knows when to de-select it (i.e. the click of my button), but I am still a new programmer and self taught which is why my knowledge of certain things is limited and I probably do really weird things, which is why I visit here to learn and fix all my weirdness. If the mouseup event is the right way to go I don't understand it as I said above.

Sorry for all the confusion...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.