我想當前的時間設定爲一個DateTimePicker(與格式時間)像如何設置當前時間在一個DateTimePicker
this.myDateTimePicker.Value = DateTime.Now;
但在執行我的代碼時,我得到一個異常
Object reference not set to an instance of an object
我做錯了什麼?
謝謝。
我想當前的時間設定爲一個DateTimePicker(與格式時間)像如何設置當前時間在一個DateTimePicker
this.myDateTimePicker.Value = DateTime.Now;
但在執行我的代碼時,我得到一個異常
Object reference not set to an instance of an object
我做錯了什麼?
謝謝。
在撥打InitializeComponent()
後,您需要輸入該密碼。直到那時,沒有myDateTimePicker
的實例。
聲明您的DateTimePicker
並嘗試。
DateTimePicker myPicker = new DateTimePicker;
myPicker.Value = DateTime.Now;
像有人所指出的,把你的代碼InitializeComponent()
之前,因爲它是在你的DateTimePicker
被初始化部分。
1 - 刪除你的控件
2 - 重新添加它。
3 - 請注意您放置代碼的位置。
之後應該繼續工作,因爲你在代碼部分正確地做了。在XAML的DatePicker的代碼添加然後
xmlns:sys="clr-namespace:System;assembly=mscorlib"
:
如果你使用WPF,不是的WinForms,添加此引用
SelectedDate="{x:Static sys:DateTime.Now}"
標籤顯示winforms ...所以不,OP不使用WPF。 – 2016-10-13 19:46:47
'myDateTimePicker'爲空。我們可以從您發佈的代碼中推斷出這一切。 – vcsjones 2012-08-02 19:23:14
很難從你發佈的代碼段中分辨出來。你能擴展你的例子嗎? – 2012-08-02 19:23:18