Archie,
Here is the final code and it works well to replace all of the Tag names with the Modbus addresses.
Public Sub FixPLCAddresses(ByVal ctrlParent As Control)
'Call this from a form to translate the PLC Tags to the Modbus addresses.
' ie Call FixPLCAddresses(me), in the Form_Load procedure.
Dim I As Integer, propertyIndex As Integer, SubIndex As Integer
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 (((p(propertyIndex).PropertyType) Is GetType(String) Or (p(propertyIndex).PropertyType) Is GetType(MfgControl.AdvancedHMI.Drivers.PLCAddressItem))) Then
'* Does this property start with "PLCAddress"?
If p(propertyIndex).Name.IndexOf("PLCAddress", StringComparison.CurrentCultureIgnoreCase) = 0 AndAlso (p(propertyIndex).PropertyType) Is GetType(String) 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))
If value <> "" Then p(propertyIndex).SetValue(ctrlParent.Controls(I), value, Nothing)
ElseIf (p(propertyIndex).PropertyType Is GetType(MfgControl.AdvancedHMI.Drivers.PLCAddressItem)) Then
'* Does the PCAddress property point to an object instance?
If p(propertyIndex).GetValue(ctrlParent.Controls(I), Nothing) IsNot Nothing Then
Dim PLCAddressObject = p(propertyIndex).GetValue(ctrlParent.Controls(I), Nothing)
Dim SubProperty() As Reflection.PropertyInfo = PLCAddressObject.GetType().GetProperties
'* Check all of the properties of the PLCAddressItem
For SubIndex = 0 To SubProperty.Length - 1
If ((SubProperty(SubIndex).PropertyType Is GetType(String) AndAlso SubProperty(SubIndex).Name.IndexOf("PLCAddress") >= 0)) Then
'* Set the value of the sub proprty
Dim subvalue As String = LookupModbusAddr(SubProperty(SubIndex).GetValue(PLCAddressObject, Nothing))
SubProperty(SubIndex).SetValue(PLCAddressObject, subvalue, Nothing)
End If
Next
End If
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
All of the PLCAddress properties get updated, however when I run my program the AnalogValueDisplay still gives an error for the original tag name.
My form has DisableSubscriptions=true on the ComComponent. In form Load I run the FixPLCAddress routine and then turn DisableSubscritions=false in code.
The only way it seems to work is if I set the ComComponent for the AnalogValueDisplay =None on the form, run the FixPLCAddress routine and then set the ComComponent and set DisableSubscritions=false in form load.
For a basic label it works straight away, but appears that the PLCAddressItem Type is treated differently on creation of the form.
Let me know if you have any thoughts on how I may be able to get around this.
Thanks,
Rich