1

I have several checkboxes in a groupbox on a vb 2008 express userform. I would like to check the checked status of each when clicking a command button and then write the checkboxes caption to be used in an email or print out. I've tried several methods and always end up with the same error "Invalid Cast Exception". I have read the help on msdn and still do not understand how to make it work. Here is the code I've been trying

Dim chk As CheckBox
    Dim sb As New System.Text.StringBuilder
    Dim names As String
    For Each chk In gbInterior.Controls
        If chk.Checked Then
            sb.Append(chk.Text)
        End If
    Next chk
    names = sb.ToString(0, sb.Length - 32)
    MsgBox(names)

I have also tried the code below but cannot figure out how to check the status and print the checkbox caption.

Dim ctl As Control
    For Each ctl In gbInterior.Controls
    If TypeOf ctl Is CheckBox Then
    MsgBox(ctl.Text & vbNewLine)
    End If
    Next ctl

Thank you for your help.

3 Answers 3

4

It looks like you are looking for the Checked property on a Control, which doesn't have a property like that.

Try declaring your variable as a CheckBox and filter your list by the control type:

For Each chk As CheckBox In gbInterior.Controls.OfType(Of CheckBox)()
  If chk.Checked Then
    sb.Append(chk.Text)
  End If
Next
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for not only providing the correct code but more importantly explaining my error. I've never had all correct answers so can I check all as answered? Now that you have helped me over this hurtle, how do I write the result to an email body? I was using msgbox for testing purposes. I am using + vbCrLf + "cabinet lock is " + cbLock.Text _ in the email body.
@UBel Change sb.Append(...) to sb.AppendLine(...), which will put each text item on a separate line. If you are asking about actually inserting it into the email body, that's a bit out of the scope of this question, but it's easy to google. Only one post can be marked as an asnwer, but if any answers help you, you can upvote any of them, and not just on your questions.
Thank you very much, this is the second time you've helped me out, your knowledge is much appreciated.
1

Your problem stems from looping through all the controls on a form and trying to set each control to a control of type checkbox. A checkbox is only a single type of control and a button can't be cast into it, so that is why you are getting casting issues.

Dim ctrl As Control
Dim sb As New System.Text.StringBuilder
Dim names As String
For Each ctrl In gbInterior.Controls
    If TypeOf ctrl Is CheckBox andalso CType(ctrl, CheckBox).Checked Then
        sb.Append(CType(ctrl, CheckBox).Text)
    End If
Next ctrl 
names = sb.ToString(0, sb.Length - 32)
MsgBox(names)

1 Comment

Thank you for the explanation of my error. I received 3 answers and all are correct and answered my question. Can I check off all as answered?
0
If TypeOf ctl Is CheckBox AndAlso CType(ctl, CheckBox).Checked Then  
   MsgBox(CType(ctl, CheckBox).Text & vbNewLine) 
End If 

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.