Solved

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

Posted on 2008-10-18
4
420 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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 …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

688 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