Solved

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

Posted on 2008-10-18
4
413 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now