Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exit the loop 4 42
VB.NET (2008) - Refactoring Question 2 21
VB.Net WebRequest Code from JSON curl 9 13
Help with preventing downloading a zip file 10 37
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 …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

809 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