Just to stick to your example as more as possible...
Your code:
Dim InitCtrl As Label
InitCtrl.Name = "lbl_T_Init_" & Microsoft.VisualBasic.Right(ProjCtrl.Name, 2)
Should change to something like:
Dim sInitCtrlName As String
sInitCtrlName = "lbl_T_Init_" & Microsoft.VisualBasic.Right(ProjCtrl.Name, 2)
Dim InitCtrl As Label = Me.Controls(sInitCtrlName)
There are even other approaches, like:
- Finding the label by its location, if each label is always at the same side and distance from its ComboBox mate.
- If the
Label and
ComboBox controls are layed inside a
TableLayoutPanel (with 2 columns and as many rows as the Label+ComboBox pairs are), you can use
GetPositionFromControl and
GetControlFromPosition methods to find the mate control from any other control.
- Having a custom ComboBox-derived class with a dedicated property of type
Label to assign at design time to the correct
Label control, though forgetting about the names.
- Assigning the
Label control as
Tag property of the
ComboBox at initialization time.
- Assigning at initialization time the same value to the property
Tag of both the
Label and
ComboBox and using it to find the matching label looping
Me.Controls colletion.
- Creating a composite
UserControl containing both a
Label and a
ComboBox , handling all events itself and exposing the internal properties, then replacing each existing pair of Label+ComboBox with one instance of this control.
- As suggested by
nilesh sawardekar, create and add the controls at run-time and during this process link them in some way.
Regards,
Daniele.