title: accessing multiple form controls using a variable for the name
body: <p>I'm trying to iterate through a group of ComboBoxes and set a property using a concatenated string &amp; variable to represent the name of the control.  However, I can't get the instance of the form to recognize the (String &amp; Integer_Variable) as one of its controls -- and so it doesn't recognize any of the appropriate properties or subroutines as members of the System.Windows.Forms.Control.</p>
  
  <p>I found the <a href="https://stackoverflow.com/q/8015094/1411373">DirectCast solution on SO</a> and it appears to work (although I'm dubious), but that feels like a very clumsy solution.  Is there a cleaner way to do this?</p>
  
  <pre class="lang-vb prettyprint-override"><code>For myTempCount = 1 To 6
      If tempValue &lt; Me.("ComboBox" &amp; myTempCount).Items.Count Then
          ComboBox.SelectedIndex = tempValue 'appears to work -- how?
          Me.ComboBox.SelectedIndex = tempValue 'appears to work
  
          Me.("ComboBox" &amp; myTempCount).SelectedIndex = tempValue 'doesn't work
          Me.Controls.("ComboBox" &amp; myTempCount).SelectedIndex = tempValue 'doesn't work
  
          DirectCast(Me.Controls.Find(("ComboBox" &amp; myTempCount), True)(0), ComboBox).SelectedIndex = tempValue 'appears to work
          DirectCast(Me.Controls("ComboBox" &amp; myTempCount), ComboBox).SelectedIndex = tempValue  'appears to work
  Next
  </code></pre>
  
  <p>This code was originally VBA / VB6, which I put through ArtinSoft's Visual Basic Upgrade Companion (VBUC).  FWIW, I'm using Microsoft Visual Basic 2010 Express.</p>