【问题标题】:Multiple countdown timer in listview C# duplicatinglistview C#复制中的多个倒数计时器
【发布时间】:2017-05-02 18:10:57
【问题描述】:

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

【问题讨论】:

    标签: c# winforms listview countdowntimer


    【解决方案1】:

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

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

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

    如果您想拥有多个DateTime,请使用 List 来存储它

        List<DateTime> _times = new List<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'");
                    }
                }
            }
        }
    }
    

    【讨论】:

    • 感谢您的回答,但是当我尝试此代码时,它会导致错误,在 void t_Tick 中显示“名称 'dateTime' 在当前上下文中不存在”。
    • 如果我尝试代码不会抛出错误。您能否描述错误发生的位置。
    • 对不起,它实际上没有抛出错误,但是当我运行代码时,倒计时仍然有以前的问题,旧的倒计时仍然被新的倒计时,等等。不过提前谢谢你。
    猜你喜欢
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    • 2016-07-21
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 2021-02-24
    相关资源
    最近更新 更多