none
Checked ListBox Preventing Item being Checked or Deleted RRS feed

  • Question

  • Hi Good People

    I have a checked List Box on My form that loads all Account Names I have in my SQL Database. So when I check one of the Boxes and click the delete Button, It gets deleted everything is ok.

    But How can I stop one Item being Checked in that checked List Box. Is there a way to disable the particular item.

    The Items in the checked list Box are company Account names, Except one which is a cash Account and should never be Deleted.

    Any Suggestions ?

    Kind Regards

    Gary 


    Gary Simpson


    • Edited by Gary Simpson Wednesday, October 25, 2017 4:25 PM
    Wednesday, October 25, 2017 3:19 PM

Answers

  • Given this

    We disallow the item "Karen" from being checked

    Public Class Form1
        Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) _
            Handles CheckedListBox1.ItemCheck
    
            If e.NewValue = CheckState.Checked Then
                Dim currentItem As String = CheckedListBox1.Items(e.Index).ToString
                If currentItem = "Karen" Then
                    e.NewValue = CheckState.Unchecked
                End If
            End If
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Gary Simpson Wednesday, October 25, 2017 5:41 PM
    Wednesday, October 25, 2017 5:00 PM
    Moderator

All replies

  • He is what I have tried So far ...

    Verion3
    
    'Private Sub cmdDeleteAccount_Click(sender As Object, e As EventArgs) Handles cmdDeleteAccount.Click
    '    SQL.ExecQuery("SELECT AccountID " & _
    '                   "FROM Accounts " & _
    '                   "WHERE CompanyName='" & clbAccounts.CheckedItems.Item(CompanyName = "CASH") & "' ")
    
    '    If clbAccounts.CheckedItems.Item(CompanyName = "CASH") Then
    '        MsgBox("You Cannot Delete this item ", MsgBoxStyle.Exclamation, "Not Allowed")
    '    Else
    '        DeleteAccount()
    
    '    End If
    
    
    'End Sub
    
    Version 2
    
    'Private Sub cmdDeleteAccount_Click(sender As Object, e As EventArgs) Handles cmdDeleteAccount.Click
    '    SQL.ExecQuery("SELECT AccountID " & _
    '                   "FROM Accounts " & _
    '                   "WHERE CompanyName='" & clbAccounts.CheckedItems.Item("CASH").ToString & "' ")
    
    '    If clbAccounts.CheckedItems.Item(CompanyName = "CASH") Then
    '        MsgBox("You Cannot Delete this item ", MsgBoxStyle.Exclamation, "Not Allowed")
    '    Else
    '        DeleteAccount()
    
    '    End If
    
    
    'End Sub
    
    Version1
    
    'Private Sub cmdDeleteAccount_Click(sender As Object, e As EventArgs) Handles cmdDeleteAccount.Click
    '    'If clbAccounts.CheckedItems.Item(CompanyName = "CASH") Then
    '    '    MsgBox("You Cannot Delete this item ", MsgBoxStyle.Exclamation, "Not Allowed")
    '    'Else
    '    '    DeleteAccount()
    
    '    'End If
    
    
    'End Sub


    Gary Simpson

    Wednesday, October 25, 2017 4:27 PM
  • Hi Gary

    Maybe this code will assist.

    Option Strict On
    Option Explicit On
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            With CheckedListBox1
                .Items.Add("ANY COMPANY")
                .Items.Add("ANOTHER COMPANY")
                .Items.Add("YET ANOTHER COMPANY")
                .Items.Add("CASH")
                .Items.Add("AND YET AGAIN ANOTHER COMPANY")
            End With
        End Sub
        Private Sub CmdDeleteAccount_Click(sender As Object, e As EventArgs) Handles cmdDeleteAccount.Click
            For Each s As String In CheckedListBox1.CheckedItems
                If Not s = "CASH" Then
                    DeleteAccount(s)
                End If
            Next
        End Sub
        Sub DeleteAccount(s As String)
            ' delete account names 's'
            Console.WriteLine(s)
        End Sub
    End Class


    Regards Les, Livingston, Scotland

    Wednesday, October 25, 2017 4:39 PM
  • Given this

    We disallow the item "Karen" from being checked

    Public Class Form1
        Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) _
            Handles CheckedListBox1.ItemCheck
    
            If e.NewValue = CheckState.Checked Then
                Dim currentItem As String = CheckedListBox1.Items(e.Index).ToString
                If currentItem = "Karen" Then
                    e.NewValue = CheckState.Unchecked
                End If
            End If
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Gary Simpson Wednesday, October 25, 2017 5:41 PM
    Wednesday, October 25, 2017 5:00 PM
    Moderator
  • Hi Karen Payne

    Thank you very Much for your Code that has Sorted out My Issue.

    I have added a message Box That let's the User Know why They cannot Delete the Selected/Current Item

    I have been on this issue all day. I have looked almost everywhere For my issue I had. But Couldn't find anything about the Issue.

    Thank You Karen

    Kind regards

    Gary

     


    Gary Simpson

    Wednesday, October 25, 2017 6:09 PM
  • Your welcome.

    BTW I didn't use a MessageBox so to keep things clear, figured you would add one :-)

    If you need to see some basics to advance code in regards to CheckedListBox and SQL-Server see my code samples on MSDN.

    VB.NET Working with CheckedListBox and SQL-Server tables


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Wednesday, October 25, 2017 6:12 PM
    Moderator