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

Hakan
Hakan used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

Commented:
Which line gives the error?

Author

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

Most Valuable Expert 2012
Top Expert 2014

Commented:
Which line is throwing the error? Can you show the full error dialog (take a screenshot)??
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial