title: Why im getting exception error indexOutOfRangeException?
body: <p>In Form1 constructor i have :</p>
  
  <pre><code>if (System.IO.File.Exists(keywords_path_file))
              {
                  ListBoxLoadKeys(LocalyKeyWords, keywords_path_file);
              }
              else
              {
                  fileExist = new StreamWriter(keywords_path_file);
                  fileExist.Close();
                  ListBoxLoadKeys(LocalyKeyWords, keywords_path_file);
              }
  </code></pre>
  
  <p>I use a breakpoint and see that the file is exist :</p>
  
  <pre><code>C:\Users\bout0_000\AppData\Local\GatherLinks\GatherLinks\Keywords\Keywords.txt
  </code></pre>
  
  <p>The file content is:</p>
  
  <pre><code>http://www.walla.co.il,walla
  http://www.cnet.com,cnet
  http://rotter.net/forum/scoops1/29961.shtml,rotter
  http://vanessawest.tripod.com/crimescenephotos.html,VanessaWest
  http://rotter.net/forum/scoops1/45227.shtml,scoops
  https://www.google.com/search?q=live+cameras,live camera
  https://www.google.com/search?q=rape+images&amp;oq=+images&amp;aqs=chrome..69i57.1661j0&amp;sourceid=chrome&amp;ie=UTF-8,hi
  https://www.google.com/search?q=+images&amp;um=1&amp;ie=UTF-8&amp;hl=en&amp;tbm=isch&amp;source=og&amp;sa=N&amp;tab=wi&amp;ei=GqotUv2kA4OftAae94DoAg&amp;biw=951&amp;bih=457&amp;sei=oaotUtDqM8WbtAag3IFg#hl=en&amp;q=+and+&amp;tbm=isch&amp;um=1,chud
  http://www.test.com,test
  </code></pre>
  
  <p>The file contain 9 keys and keywords . Left side the keys right side the keywords.</p>
  
  <p>Then its getting into this method:</p>
  
  <pre><code>private void ListBoxLoadKeys(Dictionary&lt;string, List&lt;string&gt;&gt; dictionary, string FileName)
          {
              List&lt;string&gt; urls = new List&lt;string&gt;();
              using (StreamReader sr = new StreamReader(FileName))
              {
                  while ((line = sr.ReadLine()) != null)
                  {
                      int i = line.Count();
                      tokens = line.Split(',');
                      dictionary.Add(tokens[0], tokens.Skip(1).ToList());
                      data.Add("Url: " + tokens[0] + " --- " + "Localy KeyWord: " + tokens[1]);
                      urls.Add(tokens[0]);
                  }
              }
              listBox1.DataSource = data;
              listBox1.Tag = urls;
          }
  </code></pre>
  
  <p>In the method when its getting o the line: listBox1.DataSource = data; its jumping and doing this event:</p>
  
  <pre><code>private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
          {
  
              if (listBox1.SelectedItem != null)
              {
                  label4.Text = listBox1.SelectedItem.ToString();
                  string startTag = "Url: ";
                  string endTag = " ---";
                  int startTagWidth = startTag.Length;
                  int endTagWidth = endTag.Length;
                  int index = 0;
                  index = label4.Text.IndexOf(startTag, index);
                  int start = index + startTagWidth;
                  index = label4.Text.IndexOf(endTag, start + 1);
                  string g = label4.Text.Substring(start, index - start);
                  label4.Text = g;
                  mainUrl = g;
              }
          }
  </code></pre>
  
  <p>In the end i see that data and listBox1.DataSource both contain 9 items.</p>
  
  <p>After all this the program is running.
  I see on the right in the listBox the items the keys and the keywords.
  Once i click on one of the keys in the listBox1 im getting an exception on the line:</p>
  
  <pre><code>if (listBox1.SelectedItem != null)
  </code></pre>
  
  <p>In the listBox1_SelectedIndexChanged event.</p>
  
  <p>The exception is:</p>
  
  <p>Index was outside the bounds of the array</p>
  
  <pre><code>System.IndexOutOfRangeException was unhandled
    HResult=-2146233080
    Message=Index was outside the bounds of the array.
    Source=System.Windows.Forms
    StackTrace:
         at System.Windows.Forms.ListBox.ItemArray.GetItem(Int32 virtualIndex, Int32 stateMask)
         at System.Windows.Forms.ListBox.get_SelectedItem()
         at GatherLinks.Form1.listBox1_SelectedIndexChanged(Object sender, EventArgs e) in d:\C-Sharp\GatherLinks\GatherLinks-2\GatherLinks\GatherLinks\Form1.cs:line 543
         at System.Windows.Forms.ListBox.OnSelectedIndexChanged(EventArgs e)
         at System.Windows.Forms.ListBox.WmReflectCommand(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.WmCommand(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.WmKillFocus(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>I know what the exception mean but what im getting it and how can i solve it ?</p>
  
  <p>For example if i move first when running the program with the keys arrows up and down move between the keys in the listBox and then i click on one of them im not getting any exception.
  The exception is show up when im running the program and right away first thing click with the mouse on one of the keys in the listBox.</p>
  
  <p>Moving between them and then clicking on one of them is ok but clicking on one of them first thing make the exception.</p>
  
  <p>EDIT**</p>
  
  <p>I see now that before its going to the listBox1_SelectedIndexChanged event to this event istBox1 mouse down:</p>
  
  <pre><code>private void listBox1_MouseDown(object sender, MouseEventArgs e)
          {
              if (Control.ModifierKeys == Keys.Control || ( Control.ModifierKeys == Keys.Control || e.Button == MouseButtons.Left))
              {
                  listBox1.SelectionMode = SelectionMode.MultiExtended;
              }
              else if (e.Button == MouseButtons.Left)
              {
                  listBox1.SelectionMode = SelectionMode.One;
              }
          }
  </code></pre>
  
  <p>Doing this line:</p>
  
  <pre><code>listBox1.SelectionMode = SelectionMode.MultiExtended;
  </code></pre>
  
  <p>Then back to the selectedindexchanged event and throw the exception on the line:</p>
  
  <pre><code>if (listBox1.SelectedItem != null)
  </code></pre>
  
  <p>That is also line number: 543</p>