accessing multiple form controls using a variable for the name

17,835

To answer your questions:

  1. ComboBox1.SelectedIndex works because ComboBox1 is the control that is present in the Form's ControlCollection
  2. Me.ComboBoxPrinter1.SelectedIndex works because Me is a reference to your Form class an it is referencing the Control.
  3. Me.("ComboBoxPrinter" & myTempCount).SelectedIndex doesn't work because the string ComboBoxPrinter & myTempCount is a string not a Control.
  4. Me.Controls.("ComboBoxPrinter" & myTempCount).SelectedIndex doesn't work for the same reasons.
  5. The other two instances work because you are using the string as a key to lookup and return the control which you the cast to the proper type and set your property.

I personally usually use CType other than DirectCast. The main difference according to this link between CType and DirectCast is that DirectCast has to be the exact Type where as CType can be used in narrowing or widening Conversions. DirectCast is more effiecent albeit more finicky.

That being said you could do something like this:

For myTempCount = 1 To 6
    If Controls.ContainsKey("ComboBox" & myTempCount) Then
        CType(Controls("ComboBox" & myTempCount), ComboBox).SelectedIndex = tempValue
    End If
Next

I am not using Me in front of Controls since it refers to the same collection, if your controls are in another collection you will need to use that Container instead. i.e. if you were using a Panel Panel1.Controls.ContainsKey

Share:
17,835
Wingman4l7
Author by

Wingman4l7

Did the computer science degree thing. Enjoy playing around browser plugins and APIs linked to powerful services like Coinbase or Spotify. Currently writing tools utilizing Selenium and Chromedriver to automate tasks for PayPal, Craigslist, and Amazon. Interested in what increased automation / filtering can do for services like Wikipedia.

Updated on June 04, 2022

Comments

  • Wingman4l7
    Wingman4l7 almost 2 years

    I'm trying to iterate through a group of ComboBoxes and set a property using a concatenated string & variable to represent the name of the control. However, I can't get the instance of the form to recognize the (String & 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.

    I found the DirectCast solution on SO 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?

    For myTempCount = 1 To 6
        If tempValue < Me.("ComboBox" & myTempCount).Items.Count Then
            ComboBox.SelectedIndex = tempValue 'appears to work -- how?
            Me.ComboBox.SelectedIndex = tempValue 'appears to work
    
            Me.("ComboBox" & myTempCount).SelectedIndex = tempValue 'doesn't work
            Me.Controls.("ComboBox" & myTempCount).SelectedIndex = tempValue 'doesn't work
    
            DirectCast(Me.Controls.Find(("ComboBox" & myTempCount), True)(0), ComboBox).SelectedIndex = tempValue 'appears to work
            DirectCast(Me.Controls("ComboBox" & myTempCount), ComboBox).SelectedIndex = tempValue  'appears to work
    Next
    

    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.