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?
System.Windows.Forms.CheckBox?Dim cb As CheckBoxwithDim 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.