vb.netwinformsvb.net-2010vb6-migration

accessing multiple form controls using a variable for the name


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.


Solution

  • 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