Solved

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

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

15 Experts available now in Live!

Get 1:1 Help Now