Redux: Arraylists and checkboxes

Posted on 2005-04-24
Last Modified: 2010-04-23
This is a follow-up on an earlier post; I awarded the points before testing the code, as it seemed quite straight forward.

As per (, I am "simply" trying to determine whether a checkbox has been selected in arraylist.  If so, I need to transfer that information to a new arraylist.

When I invoke the suggested code in my VB.NET program, I receive the following error:

       An unhandled exception of type 'System.InvalidCastException' occurred in program1.exe

      Additional information: Specified cast is not valid.

The code...
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cb As CheckBox

        For Each cb In g_ListofOptions
            If cb.Checked Then
                MsgBox(cb.Name & " checked")
            End If
    End Sub


The checkboxes are added to the global arraylist, g_ListofOptions in the following manner:
and subsequently, manually selected by the user.  

Will someone kindly point me to the error in my code/ways; I have spent much too much time on this problem, and alas, I am finding myself waxing nostalgic for a simplier time, and FORTRAN!

Thanks again,
Question by:DMV
    LVL 85

    Accepted Solution

    I think you might be mixing up your terminology.  You state:

        "I am "simply" trying to determine whether a checkbox has been selected in arraylist.  If so, I need to transfer that information to a new arraylist"

    but your code has:


    So you are probably not dealing with CheckBoxes stored in an ArrayList as you describe, but with a CheckedListBox control.  The two are completely different...

    Here is an example of how to determine what items are checked in a CheckedListBox:

    Public Class Form1
        Inherits System.Windows.Forms.Form

    #Region " Windows Form Designer generated code "

        Public Sub New()

            'This call is required by the Windows Form Designer.

            'Add any initialization after the InitializeComponent() call

        End Sub

        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                End If
            End If
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents CheckedListBox1 As System.Windows.Forms.CheckedListBox
        Friend WithEvents Button1 As System.Windows.Forms.Button
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.CheckedListBox1 = New System.Windows.Forms.CheckedListBox
            Me.Button1 = New System.Windows.Forms.Button
            Me.CheckedListBox1.Location = New System.Drawing.Point(64, 16)
            Me.CheckedListBox1.Name = "CheckedListBox1"
            Me.CheckedListBox1.Size = New System.Drawing.Size(216, 184)
            Me.CheckedListBox1.TabIndex = 0
            Me.Button1.Location = New System.Drawing.Point(168, 216)
            Me.Button1.Name = "Button1"
            Me.Button1.Size = New System.Drawing.Size(112, 24)
            Me.Button1.TabIndex = 1
            Me.Button1.Text = "Button1"
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(488, 334)
            Me.Name = "Form1"
            Me.Text = "Form1"

        End Sub

    #End Region

        Private g_ListofOptions As New ArrayList

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim i As Integer

            For i = 1 To 10
                g_ListofOptions.Add("Option" & i)
        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim itemChecked As String

            For Each itemChecked In CheckedListBox1.CheckedItems
        End Sub

    End Class


    Author Comment

    No doubt about it Mike, you are the diplomat. My terminology is not the only thing confused.

     Please bear with me, I am truly a fish out of water ~ but oh, so willing to learn.

    For some reason, now unknown to me, I ignored the listbox control and was working from an arraylist perspective.  Now I get it!

    Thanks so much!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now