none
Prevent CheckBox from changing state RRS feed

  • Question

  • I have 16 check boxes in a group box. The program will initialize 1 to 16 of them in the checked state. The User can check or uncheck them, but at least ONE must remain checked. That is, the User cannot uncheck all of them.  I have a Boolean array that mirrors the checkbox state as it is used elsewhere in the program.

    When it gets down to the last check box, I want to prevent this checkbox from being unchecked. The OneBoxVisible is a simple function that returns True if the number of checked boxes is greater than 1. Currently this code is replicated in each checkbox.

    Private SubChk_1_CheckedChanged(sender As Object, e As EventArgs) HandlesChk_1.CheckedChanged

    ' Make sure at least ONE box is checked - can't uncheck ALL boxes

    If NotOneBoxVisible() Then

               Chk_1.CheckState = True  ' <--- Compiler does not like this

               Exit Sub

    End If

    I was hoping to use e.Handled as in textboxes, but appears it is not available. Any ideas would be appreciated.

    Thanks

    Monday, August 19, 2019 9:56 PM

Answers

  • Turns out the solution was too simple.

    The code that did not work was:

    Chk_1.CheckState = True

    Changing it as follows does work.

    Chk_1.CheckState = CheckState.Checked

    Tuesday, August 20, 2019 1:54 PM
  • Turns out the solution was too simple.

    The code that did not work was:

    Chk_1.CheckState = True

    Changing it as follows does work.

    Chk_1.CheckState = CheckState.Checked

    This is why reading documentation, in this case on CheckedState would provide the solution.

    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, August 20, 2019 3:20 PM
    Moderator

All replies

  • Hi

    Here is one way to deal with it. This example sets the .Enabled Property of the last remaining checked checkbox to False which prevents the user from unchecking it. All checkboxes in the group box need to have the same check changed event handler.

      Sub CheckChecked()
        Dim count As Integer
        Dim cb As New CheckBox
        For Each c As Control In GroupBox1.Controls
          If c.GetType() Is GetType(CheckBox) Then
            DirectCast(c, CheckBox).Enabled = True
            If DirectCast(c, CheckBox).Checked Then
              count += 1
              cb = DirectCast(c, CheckBox)
            End If
          End If
        Next
        If count = 1 Then cb.Enabled = False
      End Sub
    
      Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged, CheckBox3.CheckedChanged, CheckBox4.CheckedChanged
        CheckChecked()
      End Sub


    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, August 19, 2019 10:24 PM
    Monday, August 19, 2019 10:24 PM
  • Hi,

    When OneBoxVisible returns False, Check1 is selected by default. When the return value is True, it does not affect the number of selections. Please try to modify the code Chk_1. CheckState = True to
    The code CheckBox 1.Checked = True.


    Public Class Form1
        Public Function OneBoxVisible(ByVal groupBox As GroupBox) As Boolean
            Dim i As Integer = 0
            For Each control As Control In groupBox.Controls
                If TypeOf control Is CheckBox Then
                    If (CType(control, CheckBox)).Checked Then
                        i += 1
                    End If
                End If
            Next
            If i >= 1 Then
                Return True
            Else
                Return False
            End If
        End Function
    
        Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged, CheckBox3.CheckedChanged, CheckBox4.CheckedChanged
            If Not OneBoxVisible(GroupBox1) Then
                CheckBox1.Checked = True
                Exit Sub
            End If
        End Sub
    End Class
    

    Best Regards,

    Julie Xu


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 20, 2019 3:05 AM
  • Hello,

    If using a fairly current version of the .NET Framework this will code ensures one check box is always checked.

    Public Class Form1
        Private CheckBoxList As List(Of CheckBox)
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            CheckBoxList = GroupBox1.Controls.OfType(Of CheckBox).ToList()
    
            CheckBoxList.ForEach(
                Sub(cb)
                    cb.CheckState = CheckState.Checked
                    AddHandler cb.CheckStateChanged, AddressOf GroupChecked
                End Sub)
        End Sub
        Private Sub GroupChecked(sender As Object, e As EventArgs)
            Dim checked = CheckBoxList.
                    Where(Function(cb) cb.CheckState = CheckState.Checked).
                    Count()
    
            Dim current As CheckBox = CType(sender, CheckBox)
    
            If checked = 0 AndAlso current.CheckState = CheckState.Unchecked Then
                current.CheckState = CheckState.Checked
            End If
    
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, August 20, 2019 3:17 AM
    Moderator
  • Probably, some of users will appreciate this feature differently, because sometimes it is inconvenient to be unable to uncheck one checkbox before checking the desired one.

    Maybe you can simplify your approach, introducing a validation and message box before accepting the form. You can also show an icon and warning label inside the form on CheckedChanged event.

    • Edited by Viorel_MVP Tuesday, August 20, 2019 8:23 AM
    Tuesday, August 20, 2019 8:21 AM
  • Turns out the solution was too simple.

    The code that did not work was:

    Chk_1.CheckState = True

    Changing it as follows does work.

    Chk_1.CheckState = CheckState.Checked

    Tuesday, August 20, 2019 1:54 PM
  • Turns out the solution was too simple.

    The code that did not work was:

    Chk_1.CheckState = True

    Changing it as follows does work.

    Chk_1.CheckState = CheckState.Checked

    This is why reading documentation, in this case on CheckedState would provide the solution.

    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, August 20, 2019 3:20 PM
    Moderator
  • Turns out the solution was too simple.

    The code that did not work was:

    Chk_1.CheckState = True

    Changing it as follows does work.

    Chk_1.CheckState = CheckState.Checked

    Hi,

    Do you resolve the issue? If you resolve the issue, please mark the helpful as answer. It will be beneficial to other community.

    Best Regards,

    Julie 


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 21, 2019 1:18 AM