我試圖播放聲音時,ListView.Items中的DateTime等於DateTime.Now。 我的代碼是:在dateView中播放聲音時dateView == dateTime.Now
public Form1()
{
InitializeComponent();
listView1.View = View.Tile;
listView1.View = View.Details;
listView1.Groups.Add(pilne);
listView1.Groups.Add(przyszle);
DateTime localDate = DateTime.Now;
foreach (ListViewItem item in listView1.Items)
{
if (item.SubItems[2].Text == DateTime.Now.ToString("dd/MMMM/yyyy"))
{
SoundPlayer simpleSound = new SoundPlayer(@"c:\Windows\Media\Alarm01.wav");
simpleSound.Play();
}
}
}
和我的第二種形式是:
public Form2()
{
InitializeComponent();
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "dd/MMMM/yyyy";
dateTimePicker2.Format = DateTimePickerFormat.Time;
dateTimePicker2.ShowUpDown = true;
}
我加入了從第二樣式的項目是這樣的:
private void btn2Zapisz_Click(object sender, EventArgs e)
{
string data2 = dateTimePicker1.Text; //date
string godzina2 = dateTimePicker2.Text; //time
string nazwa2 = tB2Nazwa.Text; //name
string opis2 = rTB2Opis.Text; //description
ListViewItem lvi = new ListViewItem(nazwa2);
lvi.SubItems.Add(data2);
lvi.SubItems.Add(godzina2);
lvi.SubItems.Add(opis2);
var dataWybrana = dateTimePicker1.Value;
var dataAktualna = DateTime.Now.AddDays(7);
string lvi2 = lvi.ToString();
if (dataWybrana < dataAktualna)
{
listViewForm2.Items.Add(lvi);
lvi.Group = pilne2;
}
else
{
listViewForm2.Items.Add(lvi);
lvi.Group = przyszle2;
}
for (int i = listViewForm2.SelectedItems.Count - 1; i >= 0; i--)
{
ListViewItem itm = listViewForm2.SelectedItems[i];
listViewForm2.Items[itm.Index].Remove();
}
this.Close();
}
它不播放聲音。當lvi.SubItems.Add(data2)等於當前日期時,我想播放聲音。這段代碼有什麼錯誤?
您是否測試過'item.SubItems [2] .Text == DateTime.Now.ToString(「dd/MMMM/yyyy」)'等同於真?它沒有發揮聲音或未能通過if語句? –
'item.SubItems [2] .Text == DateTime.Now.ToString(「dd.MMMM.yyyy」)' 無法播放聲音 – themati91
因此,if語句是否正確地返回true? –