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?
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 :
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.