Link to home
Start Free TrialLog in
Avatar of Skale
Skale

asked on

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

Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

Which line gives the error?
Avatar of Skale
Skale

ASKER

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

Which line is throwing the error? Can you show the full error dialog (take a screenshot)??
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial