列表框中的计时器c#

时间:2012-12-27 14:05:14

标签: c# timer listbox

所以我目前正在制作一个程序,我需要在列表框中的每个项目上附加一个计时器,我有这个工作,但我不能选择任何项目,是否有如何选择项目,但也有一个计时器显示列表框中的每个项目?

更新

将项目添加到新列表框时,这里是我的代码:

private void btnSchedule_Click(object sender, EventArgs e)
        {
            try
            {
                string name = lsbScheduled.SelectedItem.ToString();// saves the selected item to a string
                string newItem = (moveItem(name));//calls the method and passes the variable to it
                tmrCheckedIn.Enabled = true;
                tmrCheckedIn.Start();
                newItem += "      " + "00:00:00";
                lsbScheduled.Items.Remove(name);// removes the item from the list
                lsbCheckedIn.Items.Add(newItem); //adds the item to the list          
            }
            catch (NullReferenceException)
            {
            }
        }
here is my code for the tick event: 

 private void tmrCheckedIn_Tick(object sender, EventArgs e)
    {
        int count = lsbCheckedIn.Items.Count;

        for (int i = 0; i < count; i++)
        {
            string item = lsbCheckedIn.Items[i].ToString();
            string[] line = item.Split();
            string time = line[8];

            Time oldTime = new Time();
            oldTime.StartTime = time;

            lsbCheckedIn.Items.Remove(item);
            string newTime = string.Format(line[0] + " " + line[1] + " " +line[2] + "      " + "{0:c}", oldTime.EndTime);
            lsbCheckedIn.Items.Add(newTime);


            oldTime = null;

        }

    }

and here is my class that I use to increase the timer:
 public class Time
    {
        private int seconds, minutes, hours;
        string startTime, endTime;

       public Time()
        {
            seconds = 00;
            minutes = 00;
            hours = 00;
            startTime = " ";
            endTime = "";
        }

        public string StartTime
        {
            set { startTime = value;
            CalcNewTime();
            }
            get { return startTime; }
        }

        public string EndTime
        {
            set { endTime = value; }
            get { return endTime; }
        }

        public int Hours
        {
            set { hours = value; }
            get { return hours; }
        }

        public int Minutes
        {
            set { minutes = value; }
            get { return minutes; }
        }

        public int Second
        {
            set { seconds = value; }
            get { return seconds; }
        }

        private void CalcNewTime()
        {
            const int LIMIT = 6, CONVERT = 10;

            string[] divisions = startTime.Split(':');
            hours = Convert.ToInt32(divisions[0]);
            minutes = Convert.ToInt32(divisions[1]);
            seconds = Convert.ToInt32(divisions[2]);

            int hoursTens = hours / CONVERT;
            int hoursOnes = hours % CONVERT;
            int minutesTens = minutes / CONVERT;
            int minuteOnes = minutes % CONVERT;

            seconds += 1;
            int secondTens = seconds / CONVERT;
            int secondOnes = seconds % CONVERT;



            if (secondTens >= LIMIT)
            {
                secondTens = 0;
                secondOnes = 0;
                minutes += 1;

                minutesTens = Minutes / CONVERT;
                minuteOnes = minutes % CONVERT;
                if (minutesTens >= LIMIT)
                {
                    minutesTens = 0;
                    minuteOnes = 0;
                    hours += 1;

                    hoursTens = hours / CONVERT;
                    hoursOnes = Hours % CONVERT;
                }
            }
            endTime = Convert.ToString(hoursTens + hoursOnes + ":" + minutesTens + minuteOnes + ":" + secondTens + secondOnes);
        }

    }

使用Visual Studio 2010编写一个Windows窗体应用程序,使用您可以从工具箱中选择的计时器。我需要能够在列表框中选择项目,但我现在可以,因为我不断添加和删除列表框中的项目。我希望列表框中显示的时间可以上升,但我也需要它,以便我可以在列表框中选择项目。

1 个答案:

答案 0 :(得分:1)

您的问题是,您要从列表框中删除项目,然后添加新项目。这就是物品不会被选中的原因。相反,只需更换项目。

lsbCheckedIn.Items[i] = newTime;