-1

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?

8
  • What if you explicitly use the full name, System.Windows.Forms.CheckBox? Commented Aug 6 at 3:35
  • Thanks, I just tried this: If matches.Length > 0 AndAlso TypeOf matches(0) Is System.Windows.Forms.CheckBox Then Dim cb As CheckBox = DirectCast(matches(0), CheckBox) but still get the same error. with the DirectCast line. Commented Aug 6 at 4:29
  • Use the full name for the rest of them. Normally, you don't need to do this unless your Import statements somehow is non standard (probably due to an AI trying to be helpful) Commented Aug 6 at 4:32
  • It also gives the error "'Checked' is not a member of 'VisualStyleElement.Button.CheckBox'" for the next line of code: If cb.Checked Then Commented Aug 6 at 4:38
  • Replace Dim cb As CheckBox with Dim cb As System.Windows.Forms.CheckBox.CheckBox , or just try creating a new VB.net winform project, see the import statements and replace your current import statements with the default. Commented Aug 6 at 4:40

1 Answer 1

2

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:

  • You accidentally imported or referenced:

    Imports System.Windows.Forms.VisualStyles.VisualStyleElement.Button

  • Or possibly just:

    Imports System.Windows.Forms.VisualStyles.VisualStyleElement

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

I have 2 alternate solution to fix this problem :

  • First Option, Fully qualify CheckBox Change this,
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.

  • Second Option, Remove Conflicting Imports

    Check the top of your .vb file for something like this:

    Imports System.Windows.Forms.VisualStyles.VisualStyleElement.Button 
    

    Remove that line, so that CheckBox refers to the actual System.Windows.Forms.CheckBox.

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.