Archie,
I knew you'd come up with something, which is why I posted this.
I've seen some reflection code before, and as much as it's more difficult to read, there should be little code maintenance going forward.
I modified it a little, and put it in a function.
Here's the functions
Private Sub FixPLCAddresses(ByVal ctrlParent As Control)
For i = 0 To ctrlParent.Controls.Count - 1
Dim p() As Reflection.PropertyInfo = ctrlParent.Controls(i).GetType().GetProperties
For propertyIndex = 0 To p.Length - 1
If (p(propertyIndex) IsNot Nothing) AndAlso (Not String.IsNullOrEmpty(p(propertyIndex).Name)) AndAlso ((p(propertyIndex).PropertyType) Is GetType(String)) Then
'* Does this property start with "PLCAddress"?
If p(propertyIndex).Name.IndexOf("PLCAddress", StringComparison.CurrentCultureIgnoreCase) = 0 Then
'Dim value As String = LookupModbusAddr(Me.Controls(i).Name & "." & p(propertyIndex).Name)
Dim value As String = LookupModbusAddr(p(propertyIndex).GetValue(ctrlParent.Controls(i), Nothing))
p(propertyIndex).SetValue(ctrlParent.Controls(i), value, Nothing)
End If
End If
Next
'Fix anything in a container control, such as a panel or groupbox
If ctrlParent.Controls(i).HasChildren Then
FixPLCAddresses(ctrlParent.Controls(i))
End If
Next
End Sub
Private Function LookupModbusAddr(TagName As String) As String
'Translate the Tagname to the modbus address.
Dim value As String = ""
If (ModbusAddressLookup.TryGetValue(TagName, value)) Then
Return value
End If
Return TagName
End Function
You call the FixPLCAddresses from the form_Load like this:
Call FixPLCAddresses(Me)
The piece I added near the end will cause it to look in container controls.
This has come together nicely.
As always, Your help is much appreciated.