爲什麼我不能在字符串格式爲textboxordertostart.Text
的時間等於系統時間時播放此音頻文件? Textboxordertostart
從DateTimePicker
時間減去分鐘後得到時間。如果時間匹配播放音頻文件
我的代碼如下。
SoundPlayer myplayer = new SoundPlayer();
myplayer.SoundLocation= (@"c:\users\woolsvalley\documents\visual studio 2015\Projects\WindowsFormsApplication17\WindowsFormsApplication17\Alarm.wav");
if (DateTime.Now.ToString("hh:mm tt") == textBox_ordertostart.Text)
{
myplayer.Play();
}
該代碼會引發空異常
string formatString = "yyyHHmmss";
string sample = textBox_ordertostart.Text;
DateTime dt = DateTime.ParseExact(sample, formatString, null);
if (DateTime.Now == dt)
{ myplayer.Play(); }
這是行不通的,以及
if (DateTime.Now == DateTime.Parse(textBox_ordertostart.Text))
{ myplayer.Play(); }
「textBox_ordertostart.Text」的值是什麼?您是否已經在調試器中逐步瞭解代碼? – Tim
我會在條件行上設置一個斷點,然後手動檢查'DateTime.Now.ToString(「hh:mm tt」)'返回的結果。您應該能夠將其與您在文本框中輸入的值進行比較,並輕鬆瞭解它們不匹配的原因。 –
你的'textBox_ordertostart.Text'是否會返回一個類似於這個'00:00 PM'的值? –