/// <summary> /// Shows an event in the control. /// </summary> private void ShowValue(ref int index, ref bool overwrite, EventFieldList value, int fieldIndex) { // ignore children that are not elements. object field = value.EventFields[fieldIndex].Value; if (field == null) { return; } // get the name of the element. string name = null; if (m_monitoredItem != null) { name = m_monitoredItem.GetFieldName(fieldIndex); } // get the type name. string type = value.GetType().Name; // update the list view. UpdateList( ref index, ref overwrite, value, field, fieldIndex, name, type); }
/// <summary> /// Formats a value for display in the control. /// </summary> private string GetValueText(object value) { // check for null. if (value == null) { return("(null)"); } // format bytes. byte[] bytes = value as byte[]; if (bytes != null) { StringBuilder buffer = new StringBuilder(); for (int ii = 0; ii < bytes.Length; ii++) { if (ii != 0 && ii % 16 == 0) { buffer.Append(" "); } buffer.AppendFormat("{0:X2} ", bytes[ii]); } return(buffer.ToString()); } // format xml element. XmlElement xml = value as XmlElement; if (xml != null) { // return the entire element if not expandable. if (!IsExpandableType(xml)) { return(xml.OuterXml); } // show only the start tag. string text = xml.OuterXml; int index = text.IndexOf('>'); if (index != -1) { text = text.Substring(0, index); } return(text); } // format array. Array array = value as Array; if (array != null) { return(Utils.Format("{1}[{0}]", array.Length, value.GetType().GetElementType().Name)); } // format list. IList list = value as IList; if (list != null) { string type = value.GetType().Name; if (type.EndsWith("Collection")) { type = type.Substring(0, type.Length - "Collection".Length); } else { type = "Object"; } return(Utils.Format("{1}[{0}]", list.Count, type)); } // format encodeable object. IEncodeable encodeable = value as IEncodeable; if (encodeable != null) { return(encodeable.GetType().Name); } // format extension object. ExtensionObject extension = value as ExtensionObject; if (extension != null) { return(GetValueText(extension.Body)); } // check for event value. EventFieldList eventFields = value as EventFieldList; if (eventFields != null) { if (m_monitoredItem != null) { return(String.Format("{0}", m_monitoredItem.GetEventType(eventFields))); } return(eventFields.GetType().Name); } // check for data value. DataValue dataValue = value as DataValue; if (dataValue != null) { if (StatusCode.IsBad(dataValue.StatusCode)) { return(String.Format("{0}", dataValue.StatusCode)); } return(String.Format("{0}", dataValue.Value)); } // use default formatting. return(Utils.Format("{0}", value)); }
/// <summary> /// Formats a value for display in the control. /// </summary> private string GetValueText(object value) { // check for null. if (value == null) { return("(null)"); } // format bytes. byte[] bytes = value as byte[]; if (bytes != null) { StringBuilder buffer = new StringBuilder(); for (int ii = 0; ii < bytes.Length; ii++) { if (ii != 0 && ii % 16 == 0) { buffer.Append(" "); } buffer.AppendFormat("{0:X2} ", bytes[ii]); } return(buffer.ToString()); } // format xml element. XmlElement xml = value as XmlElement; if (xml != null) { // return the entire element if not expandable. if (!IsExpandableType(xml)) { return(xml.OuterXml); } // show only the start tag. string text = xml.OuterXml; int index = text.IndexOf('>'); if (index != -1) { text = text.Substring(0, index); } return(text); } // format array. Array array = value as Array; if (array != null) { if (array.Rank > 1) { int[] lenghts = new int[array.Rank]; for (int i = 0; i < array.Rank; ++i) { lenghts[i] = array.GetLength(i); } return(Utils.Format("{1}[{0}]", string.Join(",", lenghts), value.GetType().GetElementType().Name)); } else { return(Utils.Format("{1}[{0}]", array.Length, value.GetType().GetElementType().Name)); } } // format list. IList list = value as IList; if (list != null) { string type = value.GetType().Name; if (type.EndsWith("Collection")) { type = type.Substring(0, type.Length - "Collection".Length); } else { type = "Object"; } return(Utils.Format("{1}[{0}]", list.Count, type)); } // format encodeable object. IEncodeable encodeable = value as IEncodeable; if (encodeable != null) { return(encodeable.GetType().Name); } // format extension object. ExtensionObject extension = value as ExtensionObject; if (extension != null) { return(GetValueText(extension.Body)); } // check for event value. EventFieldList eventFields = value as EventFieldList; if (eventFields != null) { if (m_monitoredItem != null) { return(String.Format("{0}", m_monitoredItem.GetEventType(eventFields))); } return(eventFields.GetType().Name); } // check for data value. DataValue dataValue = value as DataValue; if (dataValue != null) { StringBuilder formattedValue = new StringBuilder(); if (!StatusCode.IsGood(dataValue.StatusCode)) { formattedValue.Append("["); formattedValue.AppendFormat("Q:{0}", dataValue.StatusCode); } DateTime now = DateTime.UtcNow; if ((dataValue != null) && ((dataValue.ServerTimestamp > now) || (dataValue.SourceTimestamp > now))) { if (formattedValue.ToString().Length > 0) { formattedValue.Append(", "); } else { formattedValue.Append("["); } formattedValue.Append("T:future"); } if (formattedValue.ToString().Length > 0) { formattedValue.Append("] "); } formattedValue.AppendFormat("{0}", dataValue.Value); return(formattedValue.ToString()); } // use default formatting. return(Utils.Format("{0}", value)); }