locked
ListView calculation on the second column RRS feed

  • Question

  • hello,

    I have a two columns ListView.

    When a button is clicked in the first column an intermediary time from a StopWatch will be displayed.  The text to be displayed is retrieved from 5 labels (minutes + : + seconds + : + miliseconds). In the second column I want to display the difference between the time displayed in the first column and time displayed in the previous row, first column.

    Example:

    Column 1  | Column 2

    00:00:135| 00:00:135

    00:01:255| 00:01:120

    01:00:000| 00:58:745

    I need an ideea on how to calculate the SubItem. Any help is very apreciated.

    thank you.

    Wednesday, April 13, 2011 12:42 PM

Answers

  • This one even has a label, which shows the time. But this would be even better working if creating a new seperated thread, to show the time in the label.

    But even this is cool:

        Stopwatch sw;
        System.Windows.Forms.Timer timer1;
        List<TimeSpan> tsList;
        public Form1()
        {
          InitializeComponent();
          label1.Text = "";
          listView1.Columns.Add("Column1", 100, HorizontalAlignment.Center);
          listView1.Columns.Add("Column2", -2, HorizontalAlignment.Center);
          listView1.View = View.Details;
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
          if (sw == null)
          {
            //creating new timer:
            sw = new Stopwatch();
            sw.Start();
            tsList = new List<TimeSpan>();
    
            //start new timer to show in label:
            timer1 = new System.Windows.Forms.Timer();
            timer1.Interval = 1000;
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Start();
          }
          else
          {
            sw.Stop();
            sw = null;
            listView1.Items.Clear();
            timer1.Stop();
            label1.Text = "Timer: Stopped";
          }
        }
    
        private void timer1_Tick(object sender, EventArgs e)
        {
          if (sender == timer1)
          {
            label1.Text = String.Format("Timer: {0:00}:{1:00}", sw.Elapsed.Minutes, sw.Elapsed.Seconds);
          }
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
          TimeSpan ts = sw.Elapsed;
          string time = String.Format("{0:00}:{1:00}:{2:00}", ts.Minutes, ts.Seconds, ts.Milliseconds);
    
          //adding to the list:
          tsList.Add(ts);
    
          //adding to listView:
          ListViewItem lvi = new ListViewItem(time);
          listView1.Items.Add(lvi);      
    
          //adding the difference time to the 2nd column (row above the last one).
          if (listView1.Items.Count > 1)
          {
            for (int i = 0; i < listView1.Items.Count; i++)
            {
              if (i == listView1.Items.Count - 1)
              {
                ts = ts.Subtract(tsList[i - 1]);
                listView1.Items[i - 1].SubItems.Add(String.Format("{0:00}:{1:00}:{2:00}", ts.Minutes, ts.Seconds, ts.Milliseconds));
              }
            }
          }
        }
    


    Mitja
    • Marked as answer by drake10k Friday, April 15, 2011 7:09 AM
    Wednesday, April 13, 2011 1:57 PM

All replies

  • hi,

    how do you bind your data?

    Normally you would add a property TimeDifference to your bound classes.


    Microsoft MVP Office Access
    https://mvp.support.microsoft.com/profile/Stefan.Hoffmann
    Wednesday, April 13, 2011 1:29 PM
  • I did the code you wanted. I hope you like it:

        Stopwatch sw;
        List<TimeSpan> tsList;
        public Form1()
        {
          InitializeComponent();
          listView1.Columns.Add("Column1", -2, HorizontalAlignment.Center);
          listView1.Columns.Add("Column2", -2, HorizontalAlignment.Center);
          listView1.View = View.Details;
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
          if (sw == null)
          {
            sw = new Stopwatch();
            sw.Start();
            tsList = new List<TimeSpan>();
          }
          else
          {
            sw.Stop();
            listView1.Items.Clear();
          }
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
          TimeSpan ts = sw.Elapsed;
          string time = String.Format("{0:00}:{1:00}:{2:00}", ts.Minutes, ts.Seconds, ts.Milliseconds);
    
          //adding to the list:
          tsList.Add(ts);
    
          //adding to listView:
          ListViewItem lvi = new ListViewItem(time);
          listView1.Items.Add(lvi);      
    
          //adding the difference time to the 2nd column (row above the last one).
          if (listView1.Items.Count > 1)
          {
            for (int i = 0; i < listView1.Items.Count; i++)
            {
              if (i == listView1.Items.Count - 1)
              {
                ts = ts.Subtract(tsList[i - 1]);
                listView1.Items[i - 1].SubItems.Add(String.Format("{0:00}:{1:00}:{2:00}", ts.Minutes, ts.Seconds, ts.Milliseconds));
              }
            }
          }
        }
    


    Mitja
    Wednesday, April 13, 2011 1:45 PM
  • This one even has a label, which shows the time. But this would be even better working if creating a new seperated thread, to show the time in the label.

    But even this is cool:

        Stopwatch sw;
        System.Windows.Forms.Timer timer1;
        List<TimeSpan> tsList;
        public Form1()
        {
          InitializeComponent();
          label1.Text = "";
          listView1.Columns.Add("Column1", 100, HorizontalAlignment.Center);
          listView1.Columns.Add("Column2", -2, HorizontalAlignment.Center);
          listView1.View = View.Details;
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
          if (sw == null)
          {
            //creating new timer:
            sw = new Stopwatch();
            sw.Start();
            tsList = new List<TimeSpan>();
    
            //start new timer to show in label:
            timer1 = new System.Windows.Forms.Timer();
            timer1.Interval = 1000;
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Start();
          }
          else
          {
            sw.Stop();
            sw = null;
            listView1.Items.Clear();
            timer1.Stop();
            label1.Text = "Timer: Stopped";
          }
        }
    
        private void timer1_Tick(object sender, EventArgs e)
        {
          if (sender == timer1)
          {
            label1.Text = String.Format("Timer: {0:00}:{1:00}", sw.Elapsed.Minutes, sw.Elapsed.Seconds);
          }
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
          TimeSpan ts = sw.Elapsed;
          string time = String.Format("{0:00}:{1:00}:{2:00}", ts.Minutes, ts.Seconds, ts.Milliseconds);
    
          //adding to the list:
          tsList.Add(ts);
    
          //adding to listView:
          ListViewItem lvi = new ListViewItem(time);
          listView1.Items.Add(lvi);      
    
          //adding the difference time to the 2nd column (row above the last one).
          if (listView1.Items.Count > 1)
          {
            for (int i = 0; i < listView1.Items.Count; i++)
            {
              if (i == listView1.Items.Count - 1)
              {
                ts = ts.Subtract(tsList[i - 1]);
                listView1.Items[i - 1].SubItems.Add(String.Format("{0:00}:{1:00}:{2:00}", ts.Minutes, ts.Seconds, ts.Milliseconds));
              }
            }
          }
        }
    


    Mitja
    • Marked as answer by drake10k Friday, April 15, 2011 7:09 AM
    Wednesday, April 13, 2011 1:57 PM
  • thank you.
    Friday, April 15, 2011 7:09 AM