Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Set Listview to unselected...

Posted on 2008-11-14
Medium Priority
Last Modified: 2012-05-05
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?
Question by:Hawkvalley1
  • 4
  • 3
LVL 10

Expert Comment

ID: 22965591

To clear all items you can do this:


To clear a single item:

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

Hope this helps.


LVL 48

Accepted Solution

Wayne Taylor (webtubbs) earned 200 total points
ID: 22966056
De-selecting a single item in a listview is done like this....

    ListView1.SelectedItems(0).Selected = False


Author Comment

ID: 22966556
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Author Closing Comment

ID: 31517023
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.
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 22966577
>>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?

Author Comment

ID: 22966629
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.
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 22966640
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


Author Comment

ID: 22967736
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...

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month15 days, 9 hours left to enroll

580 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