title: How to index image list by name instead of position?
body: <p>I'm very new to C# and am working on Visual Studios Express 2012 on Windows 7. </p>
  
  <p>I have an imagelist of images with the names <code>"a"</code>, <code>"b"</code>, <code>"c"</code>, and I have a combo box with the options <code>"a"</code>, <code>"b"</code>, and <code>"c"</code>.</p>
  
  <p>I'm trying to add the image corresponding to the combobox selection to a picturebox. </p>
  
  <p>Originally I was using the position rather than name like so:</p>
  
  <pre><code>         int i = comboBox1.SelectedIndex;
           pictureBox1.Image = imageList1.Images[i];
  </code></pre>
  
  <p>However, I thought there might later be changes to the combo box options and they might not be in the same order anymore, so I want to do it by name instead of position.</p>
  
  <p>I tried this: </p>
  
  <pre><code>         string name = comboBox1.SelectedText;
           int i = imageList1.Images.IndexOfKey(name);
           pictureBox1.Image = imageList1.Images[i];
  </code></pre>
  
  <p>But this results in runtime errors <code>System.ArgumentOutOfRangeException ... InvalidArgument=Value of '-1' is not valid for 'index'</code>.</p>
  
  <p>I was also thinking something like this might work:</p>
  
  <pre><code>         string grade = comboBox1.SelectedText;
           pictureBox1.Image = imageList1.ImageCollection.IndexOfKey(grade); 
  </code></pre>
  
  <p>or </p>
  
  <pre><code>         pictureBox1.Image = ImageList.ImageCollection.IndexOfKey(grade); 
  </code></pre>
  
  <p>But these give me compiler errors </p>
  
  <pre><code>'ImageCollection': cannot reference a type through an expression; 
   try 'System.Windows.Forms.ImageList.ImageCollection'
  </code></pre>
  
  <p>and </p>
  
  <pre><code>An object reference is required for the non-static field, method, or property 
  'System.Windows.Forms.ImageList.ImageCollection.IndexOfKey(string)'
  </code></pre>
  
  <p>How should I go about doing this? Suggestions for alternative methods?</p>
  
  <p>Thanks in advance.</p>