/// <summary> /// Sets the value shown in the control. /// </summary> private void SetValue(Variant value) { ValueTB.ForeColor = Color.Empty; ValueTB.Font = new Font(ValueTB.Font, FontStyle.Regular); m_textChanged = false; // check for null. if (Variant.Null == value) { ValueTB.Text = String.Empty; m_value = Variant.Null; return; } // get the source type. TypeInfo sourceType = value.TypeInfo; if (sourceType == null) { sourceType = TypeInfo.Construct(value.Value); } m_value = new Variant(value.Value, sourceType); // display value as text. StringBuilder buffer = new StringBuilder(); XmlWriter writer = XmlWriter.Create(buffer, new XmlWriterSettings() { Indent = true, OmitXmlDeclaration = true }); XmlEncoder encoder = new XmlEncoder(new XmlQualifiedName("Value", Namespaces.OpcUaXsd), writer, m_session.MessageContext); encoder.WriteVariantContents(m_value.Value, m_value.TypeInfo); writer.Close(); ValueTB.Text = buffer.ToString(); // extract the encoding id from the value. ExpandedNodeId encodingId = null; ExtensionObjectEncoding encoding = ExtensionObjectEncoding.None; if (sourceType.BuiltInType == BuiltInType.ExtensionObject) { ExtensionObject extension = null; if (sourceType.ValueRank == ValueRanks.Scalar) { extension = (ExtensionObject)m_value.Value; } else { // only use the first item in the list for arrays. ExtensionObject[] list = (ExtensionObject[])m_value.Value; if (list.Length > 0) { extension = list[0]; } } encodingId = extension.TypeId; encoding = extension.Encoding; } if (encodingId == null) { StatusCTRL.Visible = false; return; } // check if the encoding is known. IObject encodingNode = m_session.NodeCache.Find(encodingId) as IObject; if (encodingNode == null) { StatusCTRL.Visible = false; return; } // update the encoding shown. if (encoding == ExtensionObjectEncoding.EncodeableObject) { EncodingCB.Text = "(Converted to XML by Client)"; } else { EncodingCB.Text = m_session.NodeCache.GetDisplayText(encodingNode); } m_encodingName = encodingNode.BrowseName; // find the data type for the encoding. IDataType dataTypeNode = null; foreach (INode node in m_session.NodeCache.Find(encodingNode.NodeId, Opc.Ua.ReferenceTypeIds.HasEncoding, true, false)) { dataTypeNode = node as IDataType; if (dataTypeNode != null) { break; } } if (dataTypeNode == null) { StatusCTRL.Visible = false; return; } // update data type display. DataTypeTB.Text = m_session.NodeCache.GetDisplayText(dataTypeNode); DataTypeTB.Tag = dataTypeNode; // update encoding drop down. EncodingCB.DropDownItems.Clear(); foreach (INode node in m_session.NodeCache.Find(dataTypeNode.NodeId, Opc.Ua.ReferenceTypeIds.HasEncoding, false, false)) { IObject encodingNode2 = node as IObject; if (encodingNode2 != null) { ToolStripMenuItem item = new ToolStripMenuItem(m_session.NodeCache.GetDisplayText(encodingNode2)); item.Tag = encodingNode2; item.Click += new EventHandler(EncodingCB_Item_Click); EncodingCB.DropDownItems.Add(item); } } StatusCTRL.Visible = true; }