Winform 收到 CallbackOnCollectedDelegate 错误

Winform getting an error of CallbackOnCollectedDelegate

尝试清除 datatimepicker 时抛出异常。

对 'System.Windows.Forms!System.Windows.Forms.NativeMethods+WndProc::Invoke' 类型的垃圾收集委托进行了回调。这可能会导致应用程序崩溃、损坏和数据丢失。将委托传递给非托管代码时,它们必须由托管应用程序保持活动状态,直到保证它们永远不会被调用。'

错误发生在箭头下方-->

#Region " Load Controls to Clear "
    Private Sub ControlsClear()
        Try

            'Load blank defaults
            Dim a As Control
            For Each a In Me.Controls
                If TypeOf a Is TextBox Then
                    a.Text = Nothing
                End If
                If TypeOf a Is ComboBox Then
                    If Not a.Name.Contains("PrinterComboBox") Then
                        a.Text = Nothing
                    End If
                End If
                If TypeOf a Is DateTimePicker Then
                    Dim dtp As DateTimePicker = CType(a, DateTimePicker)
                    dtp.Format = DateTimePickerFormat.Custom
                    dtp.CustomFormat = " "
                End If
            Next
            For Each GroupBoxCntrol As Control In Me.Controls
                If TypeOf GroupBoxCntrol Is GroupBox Then
                    If Not GroupBoxCntrol.Name.Contains("Search") Then
                        For Each cntrl As Control In GroupBoxCntrol.Controls
                            If TypeOf cntrl Is TextBox Then
                                cntrl.Text = Nothing
                            End If
                            If TypeOf cntrl Is ComboBox Then
                                If Not cntrl.Name.Contains("PrinterComboBox") Then
                                    cntrl.Text = Nothing
                                End If
                            End If
                            If TypeOf cntrl Is DateTimePicker Then
                                Dim dtp As DateTimePicker = CType(cntrl, DateTimePicker)
                       ------>  dtp.Format = DateTimePickerFormat.Custom
                                dtp.CustomFormat = " "
                            End If
                        Next

                    End If
                End If

            Next

        Catch ex As Exception
            _log.Error(ex.ToString & vbCrLf & ex.StackTrace.ToString)
            MessageBox.Show(ex.ToString, "", MessageBoxButtons.OK, MessageBoxIcon.Error)

        Finally
            Me.Invalidate()

        End Try
    End Sub
#End Region

点击继续后出现异常未处理错误

System.NullReferenceException: 'Object reference not set to an instance of an object.'

知道为什么我的 winforms 会抛出这个错误吗?

我在顶部有这个 Implements IDisposable

提前致谢

来发现我解决了我自己的问题。这与我有两个使用相同代码尝试清除和设置格式的潜艇有关,所以我试图两次格式化相同的字段。

我在代码中添加了以下内容以检查格式是否已设置

                        If dtp.CustomFormat <> " " Then
                            dtp.Format = DateTimePickerFormat.Custom
                            dtp.CustomFormat = " "
                        End If