How to get checked items from dialog's listview in vb.net

Hello I'm using below code to get checked items but it gives "Object reference not set to instance of object" error. Does anyone knows how can i fix that?

            Try
                If BuildSetLabelID.ShowDialog() = DialogResult.OK Then
                    Dim id As String = BuildSetLabelID.cboSetID.SelectedItem.ToString
                    For Each i As ListViewItem In BuildSetLabelID.lstDataGridViews.CheckedItems
                        MsgBox(i.Name)
                    Next
                 End If
            Catch ex As Exception
                Tools.OutputLog.Print(Tools.OutputLog.Type.ERR, ex.Message)
            End Try

Open in new window


and this is entire class of dialog

Public Class BuildSetLabelID
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click

        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub DialogLoad(sender As Object, e As EventArgs) Handles MyBase.Load
        lstDataGridViews.Clear()
        FindDataGridViews(GUI.pnlModuleBuildContent)
    End Sub
    Private Sub FindDataGridViews(ByVal control As Control)
        If control.HasChildren Then
            For Each childControl As Control In control.Controls
                If (TypeOf (childControl) Is DataGridView) Then
                    Dim dgv As DataGridView = CType(childControl, DataGridView)
                    If dgv.Columns.Contains("colKey") = True Then
                        Dim item As New ListViewItem With {
                        .Text = dgv.Tag,
                        .Tag = dgv.Name
                    }
                        lstDataGridViews.Items.Add(dgv.Tag)
                    End If
                Else
                    FindDataGridViews(childControl)
                End If
            Next
        End If
    End Sub
End Class

Open in new window

HakanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Which line gives the error?
HakanAuthor Commented:
After i clicked OK button it gives this error.

This part:

  If BuildSetLabelID.ShowDialog() = DialogResult.OK Then
                    Dim id As String = BuildSetLabelID.cboSetID.SelectedItem.ToString
                    For Each i As ListViewItem In BuildSetLabelID.lstDataGridViews.CheckedItems
                        MsgBox(i.Name)
                    Next
    End If

Open in new window

Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Which line is throwing the error? Can you show the full error dialog (take a screenshot)??
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
Get rid of both "Me.Close()" statements in your BuildSetLabelID dialog class.  Setting DialogResult hides the form and returns execution to the ShowDialog() line.

Also, you're showing the default instance of the dialog by using its name directly.  You should create an instance of it and use that instead:
Try
        Dim build As New BuildSetLabelID
	If build.ShowDialog() = DialogResult.OK Then
		Dim id As String = build.cboSetID.SelectedItem.ToString
		For Each i As ListViewItem In build.lstDataGridViews.CheckedItems
			MsgBox(i.Name)
		Next
	 End If
Catch ex As Exception
	Tools.OutputLog.Print(Tools.OutputLog.Type.ERR, ex.Message)
End Try

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.