listview C#复制中的多个倒数计时器

Multiple countdown timer in listview C# duplicating

我是 C# 新手。 我正在尝试制作一个简单的任务提醒程序。 问题是,当我尝试为截止时间添加倒计时时,它无法正常工作。

我的第一个任务倒计时将被我的第二个任务倒计时覆盖,我添加第三个任务时也是如此等等。

这里是相关部分的代码。

        private void buttonSave_Click(object sender, EventArgs e)
    {
        if (this.textBox_Task.Text != "")
        {
            listView1.View = View.Details;
            ListViewItem lvwItem = listView1.Items.Add(dateTimePicker1.Text);
            var day = dateTimePicker1.Value.Day;
            var month = dateTimePicker1.Value.Month;
            var year = dateTimePicker1.Value.Year;

            endTime = new DateTime(year,month,day);

            //Console.WriteLine(day);
            //Console.WriteLine(month);
            //Console.WriteLine(year);
            //Console.WriteLine(dTime

            Timer t = new Timer();
            t.Interval = 500;
            t.Tick += new EventHandler(t_Tick);
            t.Start();

            lvwItem.SubItems.Add(textBox_Task.Text);
            lvwItem.SubItems.Add(textBox_Note.Text);
            lvwItem.SubItems.Add("");
            this.dateTimePicker1.Focus();
            this.textBox_Note.Focus();
            this.textBox_Task.Focus();
            this.textBox_Task.Clear();
            this.textBox_Note.Clear();

        }
        else
        {
            MessageBox.Show("Please enter a task to add.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.textBox_Task.Clear();
            this.textBox_Note.Clear();
        }
    }

        void t_Tick(object sender, EventArgs e)
    {
        TimeSpan ts = endTime.Subtract(DateTime.Now);
        var hari = dateTimePicker1.Value.Day;
        Console.WriteLine(ts.Days);

        for (int i = 0; i < listView1.Items.Count; i++)
        {
            if (ts.Days == 0)
            {
                listView1.Items[i].SubItems[3].Text = "DEADLINE";
            }
            else
            {
                listView1.Items[i].SubItems[3].Text = ts.ToString("d' Days 'h' Hours 'm' Minutes 's' Seconds to go'");
            }
        }

    }

任何愿意提供帮助的人都将不胜感激。 提前致谢。

Here is the link to the picture of my problem

您现在正在做的是在每次单击按钮时用一个新对象覆盖当前的 endTime 对象,例如:

endTime = new DateTime(year,month,day); 

如果您将新的 DateTime 对象分配给 endTime。你覆盖旧的。因此,第一次单击按钮将起作用,但第二次单击将创建一个 DateTime 的新对象并将其分配给 to endTime。接下来,您将计算 one 对象 DateTime 的时差。所以逻辑上每个列表视图项目都是同一时间

如果你想有多个DateTime使用一个List存储在like

    List<DateTime> _times = new List<DateTime>();

在按钮单击方法中将 DateTime 添加到列表中

  // here add the datetime to the list
  DateTime dateTime = new DateTime(year, month, day);
   _times.Add(dateTime);

接下来您可以循环遍历日期并在 tick 方法中为每个日期计算时差:

        foreach (var dateTime in _times)
        {
            TimeSpan ts = dateTime.Subtract(DateTime.Now);
            // etc..
        }

您还为每次 500 毫秒后计算创建了一个计时器。您现在可以使用一个计时器,这比每次都装一个计时器更有效。只需在构造函数中分配它

  public Form1()
  {
        InitializeComponent();

        Timer t = new Timer();
        t.Interval = 500;
        t.Tick += new EventHandler(t_Tick);
        t.Start();
   }

完整代码

public partial class Form1 : Form
{
    // This is the list where  the DateTimes are stored so you can have more values
    List<DateTime> _times = new List<DateTime>();
    public Form1()
    {
        InitializeComponent();

        // Assign the timer here
        Timer t = new Timer();
        t.Interval = 500;
        t.Tick += new EventHandler(t_Tick);
        t.Start();
    }

    private void buttonSave_Click(object sender, EventArgs e)
    {
        if (this.textBox_Task.Text != "")
        {
            listView1.View = View.Details;
            ListViewItem lvwItem = listView1.Items.Add(dateTimePicker1.Text);
            var day = dateTimePicker1.Value.Day;
            var month = dateTimePicker1.Value.Month;
            var year = dateTimePicker1.Value.Year;

            // Add Datetime to list
            DateTime dateTime = new DateTime(year, month, day);
            _times.Add(dateTime);

            lvwItem.SubItems.Add(textBox_Task.Text);
            lvwItem.SubItems.Add(textBox_Note.Text);
            lvwItem.SubItems.Add("");
            this.dateTimePicker1.Focus();
            this.textBox_Note.Focus();
            this.textBox_Task.Focus();
            this.textBox_Task.Clear();
            this.textBox_Note.Clear();

        }
        else
        {
            MessageBox.Show("Please enter a task to add.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.textBox_Task.Clear();
            this.textBox_Note.Clear();
        }
    }
    void t_Tick(object sender, EventArgs e)
    {
        // loop thru all datetimes and calculate the diffrence
        foreach (var dateTime in _times)
        {
            // Call the specific date and subtract on it
            TimeSpan ts = dateTime.Subtract(DateTime.Now);

            var hari = dateTimePicker1.Value.Day;
            Console.WriteLine(ts.Days);

            for (int i = 0; i < listView1.Items.Count; i++)
            {
                if (ts.Days == 0)
                {
                    listView1.Items[i].SubItems[3].Text = "DEADLINE";
                }
                else
                {
                    listView1.Items[i].SubItems[3].Text = ts.ToString("d' Days 'h' Hours 'm' Minutes 's' Seconds to go'");
                }
            }
        }
    }
}