0

所以我目前正在开发一个程序,在该程序中,我需要在列表框中的每个项目上附加一个计时器,我有这个工作,但我无法选择任何项目,有没有办法能够选择项目,但也有一个计时器显示到列表框中的每个项目?

更新:

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

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 窗体应用程序时,使用可以从工具箱中选择的计时器。我需要能够选择列表框中的项目,但现在我可以,因为我不断地从列表框中添加和删除项目。我希望列表框中显示的时间增加,但我也需要它,以便我可以选择列表框中的项目。

4

1 回答 1

1

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

lsbCheckedIn.Items[i] = newTime;
于 2012-12-27T14:40:30.140 回答