• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 292
  • Last Modified:

Redux: Arraylists and checkboxes

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 (http://experts-exchange.com/Programming/Q_21399992.html), 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
        Next
    End Sub

+++++++++++++++++++++++++++++++++++++++++++++++

The checkboxes are added to the global arraylist, g_ListofOptions in the following manner:
              chkListBox.Items.AddRange(g_ListofOptions.ToArray)
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,
Deb
0
DMV
Asked:
DMV
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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:

    chkListBox.Items.AddRange(g_ListofOptions.ToArray)

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()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        '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
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    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.SuspendLayout()
        '
        'CheckedListBox1
        '
        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
        '
        'Button1
        '
        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"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(488, 334)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.CheckedListBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    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)
        Next
        CheckedListBox1.Items.AddRange(g_ListofOptions.ToArray)
    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
            Debug.WriteLine(itemChecked)
        Next
    End Sub

End Class

0
 
DMVAuthor Commented:
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!
Deb
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now