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

时间:2017-05-02 18:10:58

标签: c# winforms listview countdowntimer

我是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

1 个答案:

答案 0 :(得分:0)

您现在正在做的是在每个按钮上单击覆盖当前的endTime对象,如:

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

如果您将新的DateTime对象分配给endTime。你重写旧的。因此,第一个按钮单击将起作用,但第二个按钮将创建DateTime的新对象并为其分配to endTime。接下来,您要计算一个对象DateTime的时差。因此逻辑上每个listview项目都是同一时间

如果您想拥有多个DateTime,请使用列表将其存储在

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

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

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

接下来,你可以循环通过日期并计算每个时间差的方法:

        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'");
                }
            }
        }
    }
}