vb.netvariablescontrols

How do I reference controls with a variable in visual basic 2025?


I am trying to do the same sort of thing as the OP this question, but can't get the suggested code in the answer to work.

I'm using Visual Studio 2022, but doubt that's the reason it doesn't work for me. I have designed a form with an array of checkboxes, named CheckBox1 through to CheckBox48. I want to save the state of each checkbox when the form is closed (and then restore it when it's opened again). So to make the code compact, I'm trying to use a loop to get the status of each checkbox and then I plan to store that in a dictionary. The saving/restoring isn't done yet, just trying to check the current state of each checkbox for now!

So here's my code:

Public Class FormQuestions
    Private controlStates As New Dictionary(Of String, Object)
    Private Sub FormQuestions_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
        Dim x As Integer, y As Integer, intIndex As Integer, bState As Boolean
        Dim matches() As Control
        ' Save the state of each CheckBox to the controlStates dictionary
        For x = 0 To 5
            For y = 1 To 8
                intIndex = (x * 8) + y
                matches = Me.Controls.Find("CheckBox" & intIndex, True)
                If matches.Length > 0 AndAlso TypeOf matches(0) Is CheckBox Then
                    Dim cb As CheckBox = DirectCast(matches(0), CheckBox)
                    If cb.Checked Then
                        'insert code to save the state of checkbox in controlStates dictionary
                    End If
                End If
            Next
        Next
    End Sub
End Class

When I try to run the code, I get the error "Expression of type 'Control' can never be of type 'VisualStyleElement.Button.CheckBox'"

Any idea why it doesn't work?


Solution

  • The problem is Expression of type 'Control' can never be of type 'VisualStyleElement.Button.CheckBox'
    Suggest that somewhere in your code you're trying to cast a Control to the wrong CheckBox type, not System.Windows.Forms.CheckBox, but instead to System.Windows.Forms.VisualStyles.VisualStyleElement.Button.CheckBox, which is not a control at all but a style descriptor.

    The root cause, this line is fine
    If matches.Length > 0 AndAlso TypeOf matches(0) Is CheckBox Then

    However, what likely happened is:

    Which caused CheckBox to refer to VisualStyleElement.Button.CheckBox, not System.Windows.Forms.CheckBox.

    I have 2 alternate solution to fix this problem :

    If matches.Length > 0 AndAlso TypeOf matches(0) Is CheckBox Then
        Dim cb As CheckBox = DirectCast(matches(0), CheckBox)
    

    to:

    If matches.Length > 0 AndAlso TypeOf matches(0) Is System.Windows.Forms.CheckBox Then
        Dim cb As System.Windows.Forms.CheckBox = DirectCast(matches(0), System.Windows.Forms.CheckBox)
    

    This avoids any confusion about what CheckBox means.

    The Fixed Code :

    Private Sub FormQuestions_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
        Dim x As Integer, y As Integer, intIndex As Integer
        Dim matches() As Control
        ' Save the state of each CheckBox to the controlStates dictionary
        For x = 0 To 5
            For y = 1 To 8
                intIndex = (x * 8) + y
                matches = Me.Controls.Find("CheckBox" & intIndex, True)
                If matches.Length > 0 AndAlso TypeOf matches(0) Is System.Windows.Forms.CheckBox Then
                    Dim cb As System.Windows.Forms.CheckBox = DirectCast(matches(0), System.Windows.Forms.CheckBox)
                    controlStates(cb.Name) = cb.Checked
                End If
            Next
        Next
    End Sub
    

    This will correctly store the .Checked state of each CheckBox into the controlStates dictionary, using its name as the key.

    If you have any question, fell free to respond this answer.