简体   繁体   中英

Timer inside a listbox c#

So I am currently working on a program in which I need to have a timer attached to each item inside of a list box, I have that working, but I can't select any of the items, is there a way to be able to select the items but also have a timer displayed to each item in the list box?

Update:

when adding the item to a new list box here is the code that I have:

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

    }

in programming a windows form application using visual studio 2010 im using the timer that you can select from the toolbox. I need to be able to select the items in the list box but right now i can because im constantly adding and removing items from the list box. I want the time that is displayed in the list box to go up, but i also need it so that i can select the item in the list box.

Your problem is that you're removing items from the listbox, and then adding new ones. This is why items do not remain selected. Instead, just replace the items.

lsbCheckedIn.Items[i] = newTime;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM