嘗試清除 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:“物件參考未設定為物件的實體。”
知道為什么我的 winforms 會拋出此錯誤嗎?
我確實有這個在頂部
Implements IDisposable
提前致謝
uj5u.com熱心網友回復:
來發現我解決了我自己的問題。這與我有兩個具有相同代碼的潛艇試圖清除和設定格式有關,所以我試圖對相同的欄位進行兩次格式化。
我在代碼中添加了以下內容以檢查格式是否已設定
If dtp.CustomFormat <> " " Then
dtp.Format = DateTimePickerFormat.Custom
dtp.CustomFormat = " "
End If
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/465209.html
標籤:表格
