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

x
?
Solved

How to restrict a listview with checkboxes to only one selection

Posted on 2007-10-09
5
Medium Priority
?
529 Views
Last Modified: 2013-11-26
I am using VS2005 and have a listview control that is set to display checkboxes and I am only wanting the user to be able to select only one of the checkboxes in the list.  I thought that setting the multiselect property to false would have done this but it only stops the user from highlighting more than one of the items.  Is there a property that I am missing?
0
Comment
Question by:MELeBlanc
  • 3
  • 2
5 Comments
 
LVL 27

Expert Comment

by:VBRocks
ID: 20041423
You can do it programmatically, using the ItemChecked event of the ListView, for example:

    Private Sub ListView1_ItemChecked(ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles ListView1.ItemChecked

       'Loop through each item in the list and check to see if it is checked and does not have the focus
        For i As Int16 = 0 To Me.ListView1.Items.Count - 1
            If Me.ListView1.Items(i).Checked = True AndAlso _
                Me.ListView1.Items(i).Focused = False Then

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

            End If

        Next
    End Sub

0
 
LVL 48

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 2000 total points
ID: 20041514
Hi MELeBlanc,

There is no property you can set, but you can capture the ItemChecked event, and clear all others....

    Private UnCheckingItems As Boolean = False

    Private Sub ListView1_ItemChecked(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles ListView1.ItemChecked
        If UnCheckingItems Then Exit Sub
        UnCheckingItems = True
        For Each lvi As ListViewItem In ListView1.CheckedItems
            If lvi IsNot e.Item Then
                lvi.Checked = False
            End If
        Next
        UnCheckingItems = False
    End Sub

Regards,

Wayne
0
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 20041534
VBRocks - I tried yours but it would only work if the required item was selected prior to checking the box.
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 20041807
You're right...  I created the listview without adding the Text to the items, so it worked for me.  I went
back and added the Text, and then found that to be the case...



0
 
LVL 27

Expert Comment

by:VBRocks
ID: 20041837
However, including the modification to check the Item, it works great:

    Private Sub ListView1_ItemChecked(ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles ListView1.ItemChecked

        'Loop through each item in the list and check to see if it is checked and does not have the focus
        For i As Int16 = 0 To Me.ListView1.Items.Count - 1
            If Me.ListView1.Items(i).Checked = True AndAlso _
                Me.ListView1.Items(i) IsNot e.Item Then

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

            End If

        Next

    End Sub

0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
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.
Loops Section Overview
Suggested Courses

571 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