Solved

vb.net 2005: can't get checklistbox items to check/uncheck

Posted on 2008-10-18
4
418 Views
Last Modified: 2012-05-05
I use the following code to load a CBL with a list of files, each of which the user should be able to check/uncheck:

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each file As String In Directory.GetFiles(path, "*.txt")
            If (Not file.EndsWith("xxx.txt")) Then
                clb1.Items.Add(file.Substring(file.LastIndexOf("\") + 1), False)
            End If
        Next
    End Sub

The following code allows the user to check an item, but does not allow unchecking.  GetItemChecked seems to return false even after the item is checked.  Any help?  I'm surprised I am writing any code, I thought the list would have supported checking/unchecking by default.

    Private Sub clb1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clb1.SelectedIndexChanged
        If (clb1.SelectedIndex <> -1) Then
            If (clb1.GetItemChecked(clb1.SelectedIndex)) Then
                clb1.SetItemChecked(clb1.SelectedIndex, False)
            Else
                clb1.SetItemChecked(clb1.SelectedIndex, True)
            End If
        End If
    End Sub

Thanks for any help.
0
Comment
Question by:ottenm
  • 2
4 Comments
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 22750120
Try using the ItemCheck event instead....

    Private Sub clb1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles clb1.ItemCheck
        If e.NewValue = CheckState.Unchecked Then
            e.NewValue = CheckState.Checked
        End If
    End Sub

Wayne
0
 
LVL 48

Accepted Solution

by:
jpaulino earned 500 total points
ID: 22750220
>>  I'm surprised I am writing any code, I thought the list would have supported checking/unchecking by default.

And it supports!

Change the property CheckOnClick to True and remove all your code.


Me.clb1.CheckOnClick = True

Open in new window

0
 

Author Comment

by:ottenm
ID: 22754387
Thanks for both inputs, the second is definitely the shortest path, had no idea there was such a property, and even less of an idea as to why "true" is not the default behavior.
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 22755629
>> and even less of an idea as to why "true" is not the default behavior.
Neither do I ...
Glad I could help!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Advice in Xamarin 21 96
Winform Module - What is the ASP.Net equiv 2 28
FInd Image Control Gridview 3 20
Name Space error VS2015 1 22
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

808 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