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.
Something like this:
I came across this issue again with multiple controls of differing types that needed the same operation performed on a common property (like
.Text
). Since you cannot use a variable to represent the control type parameter inCType()
, you'll have to use a conditional and a corresponding hardcodedCType()
command to get the control. This is what I came up with:controlName
is the concatenated string name. So, you can use this Function pretty much the same way the previous answers usedCType()
:Ouch!!! I messed with Direct Cast once. I remember it being a nightmare. My preference is to either stick with server side controls, or write them in as client side Javascript/Ajax. Where at in your above code is it failing? Any inner exceptions?
To answer your questions:
ComboBox1.SelectedIndex
works because ComboBox1 is the control that is present in the Form's ControlCollectionMe.ComboBoxPrinter1.SelectedIndex
works because Me is a reference to your Form class an it is referencing the Control.Me.("ComboBoxPrinter" & myTempCount).SelectedIndex
doesn't work because the stringComboBoxPrinter & myTempCount
is a string not a Control.Me.Controls.("ComboBoxPrinter" & myTempCount).SelectedIndex
doesn't work for the same reasons.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:
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
Maybe you can try something like this (C#):
Here is the above code converted to VB.NET using online tool:
I hope this helps!