0

我一直在試圖弄清楚如何使兩個值匹配,並無濟於事,每次都得到不匹配的時間。當使用此代碼時,我來得非常接近:如何正確比較DateTime.Now和DateTimePicker.Value並匹配兩個值?

this.dateOfBirthPicker.Value = DateTime.Now; 

但它在匹配彼此的值時已經秒了一秒鐘。我環顧四周,注意到經常彈出日期,但它說我缺少一個程序集引用,是否使用System.DateTime?因爲我補充說,仍然沒有工作。我不知道還有什麼可以嘗試,並偷偷摸摸的幫助。當我嘗試更改時間或刪除時間並僅使用僅限日期格式時,DateTimePicker會很痛苦。

如果用戶沒有更改或設置DateOfBirthPicker設置爲DateTime.Now(默認),那麼用戶將被要求這樣做,這就是我想要做的。

C#代碼:

DateTime dtN = DateTime.Now; // create and initialize DateTime.Now 
     this.dateOfBirthPicker.Value = DateTime.Now; // set current time for DateTimePicker to match dtN 
     DateTime date = dateOfBirthPicker.Value.Date; // ERROR: missing directive or assembly reference 
     if(dateOfBirthPicker.Value == dtN) 
     { 
      dateOfBirthTBL.Text = "Date of Birth: *"; 
      dateOfBirthTBL.Foreground = new SolidColorBrush(Colors.Red); 
      dateOfBirthTBL.FontWeight = FontWeights.Bold; 
     } 

回答

1

通過你的問題的聲音,你綁到實現一個校驗,如果用戶沒有改變dateOfBirthPicker的值,然後突出顯示紅色的嗎?

而不是設置的:

this.dateOfBirthPicker.Value = DateTime.Now; 

當加載的形式設置一個全局變量來DateTime.Now,您的日期選擇器值設置爲這個變量,並用它在你的函數來比較上面:

_dateNow = DateTime.Now; 
this.dateOfBirthPicker.Value = _dateNow; 

然後做這樣的比較:

if(dateOfBirthPicker.Value == _dateNow) 
{ 
    dateOfBirthTBL.Text = "Date of Birth: *"; 
    dateOfBirthTBL.Foreground = new SolidColorBrush(Colors.Red); 
    dateOfBirthTBL.FontWeight = FontWeights.Bold; 
} 
+0

謝謝!我現在明白了! – TheAmazingKnight

相關問題