title: Im getting ArgumentOutOfRange exception when trying to color text in a listBox why?
body: <p>This is the class :</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Drawing;
  using System.Windows.Forms;
  
  namespace GatherLinks
  {
      class ColorText
      {
  
  
          public static void Texts(RichTextBox box, string text, Color color)
          {
              box.SelectionStart = box.TextLength;
              box.SelectionLength = 0;
  
              box.SelectionColor = color;
              box.AppendText(text);
              box.SelectionColor = box.ForeColor;
          }
  
          public static void ColorListBox(List&lt;string&gt; data, DrawItemEventArgs e)
          {
              string strLeft = null;
              string strMid = "---";
              string strRight = null;
              if (data[e.Index].Contains(strMid))
              {
                  int index = data[e.Index].IndexOf(strMid);
                  strLeft = data[e.Index].Substring(0, index);
                  strRight = data[e.Index].Substring(index + strMid.Length);
              }
  
              using (Font f = new Font(FontFamily.GenericSansSerif, 16, FontStyle.Regular))
              {
                  float startPos;
                  e.Graphics.DrawString(strLeft, f, Brushes.Red, e.Bounds.X, e.Bounds.Y);
                  startPos = e.Graphics.MeasureString(strLeft, f).Width;
                  e.Graphics.DrawString(strMid, f, Brushes.Black, e.Bounds.X + startPos, e.Bounds.Y);
                  startPos = e.Graphics.MeasureString(strLeft + strMid, f).Width;
                  e.Graphics.DrawString(strRight, f, Brushes.Green, e.Bounds.X + startPos, e.Bounds.Y);
              }
          }
      }
  }
  </code></pre>
  
  <p>Im using the ColorListBox method.</p>
  
  <p>In Form1 i have:</p>
  
  <pre><code>private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
          {
              if (e.Index == -1)
              {
              }
              else
              {
  
                  ColorText.ColorListBox(data, e);
  
              }
          }
  </code></pre>
  
  <p>Now when the exception is show up:</p>
  
  <p>I see that the variable data is <code>count = 0</code>
  The exception is on this line:</p>
  
  <pre><code>if (data[e.Index].Contains(strMid))
  
  Index was out of range. Must be non-negative and less than the size of the collection
  
  System.ArgumentOutOfRangeException was unhandled
    HResult=-2146233086
    Message=Index was out of range. Must be non-negative and less than the size of the collection.
  Parameter name: index
    Source=mscorlib
    ParamName=index
    StackTrace:
         at System.ThrowHelper.ThrowArgumentOutOfRangeException()
         at System.Collections.Generic.List`1.get_Item(Int32 index)
         at GatherLinks.ColorText.ColorListBox(List`1 data, DrawItemEventArgs e) in d:\C-Sharp\HardwareMonitoring\HardwareMonitoring\Hardwaremonitoring\ColorText.cs:line 29
         at HardwareMonitoring.Form1.listBox1_DrawItem(Object sender, DrawItemEventArgs e) in d:\C-Sharp\HardwareMonitoring\HardwareMonitoring\Hardwaremonitoring\Form1.cs:line 394
         at System.Windows.Forms.ListBox.OnDrawItem(DrawItemEventArgs e)
         at System.Windows.Forms.ListBox.WmReflectDrawItem(Message&amp; m)
         at System.Windows.Forms.ListBox.WndProc(Message&amp; m)
         at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
         at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
         at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
         at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
         at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
         at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message&amp; m)
         at System.Windows.Forms.Control.WmOwnerDraw(Message&amp; m)
         at System.Windows.Forms.Control.WmDrawItem(Message&amp; m)
         at System.Windows.Forms.Control.WndProc(Message&amp; m)
         at System.Windows.Forms.ScrollableControl.WndProc(Message&amp; m)
         at System.Windows.Forms.ContainerControl.WndProc(Message&amp; m)
         at System.Windows.Forms.Form.WndProc(Message&amp; m)
         at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
         at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
         at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
         at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
         at System.Windows.Forms.NativeWindow.DefWndProc(Message&amp; m)
         at System.Windows.Forms.Control.DefWndProc(Message&amp; m)
         at System.Windows.Forms.Control.WndProc(Message&amp; m)
         at System.Windows.Forms.ListBox.WndProc(Message&amp; m)
         at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
         at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
         at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    InnerException: 
  </code></pre>
  
  <p>How can i fix it so it won't throw this exception again ? Is that since i never send any info to the data variable so it's <code>count = 0</code> and that's why the exception show up ?</p>
  
  <p>Thanks in advance.</p>
  
  <p>EDIT*</p>
  
  <p>This is the backgroundworker dowork event in Form1:</p>
  
  <pre><code>private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
          {
              BackgroundWorker worker = sender as BackgroundWorker;
              while (true)
              {
  
                  if ((worker.CancellationPending == true))
                  {
                      e.Cancel = true;
                      break;
                  }
                  else
                  {
                      if (tempCpuValue &gt;= (float?)nud1.Value || tempGpuValue &gt;= (float?)nud1.Value)
                      {
                          soundPlay = true;
                          NudgeMe();
                      }
                      else
                      {
                          soundPlay = false;
                          stop_alarm = true;
  
                      }
  
                      tempCpuValue = Core.cpuView(pauseContinueDoWork,cpu,this,data,tempCpuValue,button1);
                      tempGpuValue = Core.gpuView(pauseContinueDoWork,data,tempGpuValue,button1);
                      this.Invoke(new Action(() =&gt; data = new List&lt;string&gt;()));
                      tempCpuValue = Core.cpuView(pauseContinueDoWork, cpu, this, data, tempCpuValue, button1);
                      tempGpuValue = Core.gpuView(pauseContinueDoWork, data, tempGpuValue, button1);
                      this.Invoke(new Action(() =&gt; listBox1.DataSource = null));
                      this.Invoke(new Action(() =&gt; listBox1.DataSource = data));
  
  
  
                      //listBox1.DataSource = data;
  
                  }
              }
          }
  </code></pre>
  
  <p>I can't find anywhere where do i add or put something into the List 
  Also when i'm running my program the listBox is empty.</p>
  
  <p>Could be this is the reason for the exception since data is empty and also when im doing listBox1.DataSource = data so in fact the listBox is empty ?</p>