Checkboxlist

bmjustice

Newcomer
Joined
Apr 4, 2011
My goal is to create an application that determines which check boxes ARE NOT checked in a checkedlistbox and run a command based off of those unchecked check boxes.

I have only been able to find the opposite - running a command based off of checked check boxes. Here is my code right now:

Code:
Imports Microsoft.Win32
Imports System.IO

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim _RegistryKey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList")
        For Each _KeyName As String In _RegistryKey.GetSubKeyNames()
            Using SubKey As RegistryKey = _RegistryKey.OpenSubKey(_KeyName)
                Dim _Users, _Username As String
                _Users = DirectCast(SubKey.GetValue("ProfileImagePath"), String)
                _Username = System.IO.Path.GetFileNameWithoutExtension(_Users)
                CheckedListBox1.Items.Add(_Username)
                CheckedListBox1.Items.Remove("systemprofile")
                CheckedListBox1.Items.Remove("LocalService")
                CheckedListBox1.Items.Remove("NetworkService")
                CheckedListBox1.Items.Remove("Administrator")
                CheckedListBox1.Items.Remove("administrator")
            End Using
        Next

    End Sub

    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        System.Diagnostics.Process.GetCurrentProcess.CloseMainWindow()
        System.Diagnostics.Process.GetCurrentProcess.Close()
        System.Diagnostics.Process.GetCurrentProcess.Kill()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Display in a message box all the items that are checked.
        Dim itemChecked As Object
        Const quote As String = """"


        For Each itemChecked In CheckedListBox1.CheckedItems
            ' Use the IndexOf method to get the index of an item.
            MessageBox.Show("User account:" + quote + itemChecked.ToString + quote + _
                            " will be added to the User State Migration.")

        My.Computer.FileSystem.DeleteDirectory("C:\Users\" & itemChecked.ToString(), FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin)

        Next
        Me.Close()
    End Sub
End Class

As you can see it only deletes the directory based off of the checked check boxes. Anyone have any idea on how to do the opposite? I have tried multiple False combinations. I am using Visual Studio 2010. Thanks. :D
 
Last edited by a moderator:

snarfblam

Ultimate Contributor
Joined
Jun 10, 2003
Location
USA
Well, instead of only looping over the checked items, why not loop all items and use an "if" statement to test each item and see whether it's checked. Assuming you have no duplicates in your list, you can use this little bit of code to test whether an item is checked.

Code:
Dim ItemIsChecked As Boolean = CheckedListBox1.CheckedItems.Contains([COLOR="Blue"][I]item[/I][/COLOR])
 

bmjustice

Newcomer
Joined
Apr 4, 2011
Ok thanks, I'll try it out. What would I put in the contains field (item)? The check boxes are generated based off of the users on the computer so every computer that uses this application will have different check box options. Can you give me an example of this and an IF statement to check for unchecked check boxes? Thanks.
 
Top Bottom