error when get the checked value of checked listbox array
"Object reference not set to an instance of an object."
what is the problem in my code

Public Class Form1

    Dim c1b1 As CheckedListBox()
    Private Sub btnAddItems_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim l As Integer = 5
        For i As Integer = 1 To 4
            ReDim Preserve c1b1(i)
            c1b1 = New CheckedListBox(i) {}
            c1b1(i) = New CheckedListBox
            c1b1(i).Size = New Size(75, 30)
            addii(c1b1(i))
            c1b1(i).Location = New Point(5, (l + 25) * i)
            Me.Controls.Add(c1b1(i))
        Next
    End Sub

    Private Sub btnGetCheckedItems_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim o As Object
        For i As Integer = 1 To 4
            For Each o In c1b1(i).CheckedItems
                MsgBox(o.ToString & " in c" & i.ToString)
            Next o
        Next
    End Sub

   
    Sub addii(ByVal h As CheckedListBox)
        h.Items.Add("1")
        h.Items.Add("2")
        h.Items.Add("3")
        h.Items.Add("4")

    End Sub

    
End Class

please help

Recommended Answers

All 6 Replies

Changes:

Dim l As Integer = 5
        c1b1 = New CheckedListBox(3) {}  '<= 
        For i As Integer = 0 To 3
            ReDim Preserve c1b1(i)
            c1b1(i) = New CheckedListBox
            c1b1(i).Size = New Size(75, 30)
            addii(c1b1(i))
            c1b1(i).Location = New Point(5, (l + 25) * i)
            Me.Controls.Add(c1b1(i))
        Next
Dim o As Object
        For i As Integer = 0 To 3    '<=
            For Each o In c1b1(i).CheckedItems
                If IsNothing(o) = False Then    '<=
                    MsgBox(o.ToString & " in c" & i.ToString)
                End If
            Next o
        Next

why (3)

c1b1 = New CheckedListBox(3) {}  '<=

Ok , thanks
I'm understand now the problem
but I want to increment array debending on user request
ex, suppose i have button(new checked list box)

every time user click on it

c1b1 = New CheckedListBox(i) {}  
ReDim Preserve c1b1(i)      
c1b1(i) = New CheckedListBox

thats mean , I don't know what is size of array in the begining
that code cancel old array and create new array
is there any code to preserve array and change size only ..??

List generic,

Dim items As New List(Of CheckedListBox)
        items.Add(New CheckedListBox)
        items.Add(New CheckedListBox)
        ....

ok .. thanks

I try on it ..

I write this code

Dim c1b1 As CheckedListBox()=new checkedlistbox() {}

Private Sub nextbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nextbutton.Click
ReDim Preserve c1b1(i)      
c1b1(i) = New CheckedListBox
end sub

so,my broblem is solved.

is that code cause any problem in the future ??

if that code cause problems , i will create list ..
thanks datapost ..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.