public ConvertToString ( ITypeDescriptorContext context, |
||
context | ITypeDescriptorContext | |
culture | ||
value | object | |
return | string |
/* * static void ReLayout(Control parent_control) * { * foreach (Control control in parent_control.Controls) * { * ReLayout(control); * * control.ResumeLayout(false); * control.PerformLayout(); * } * * parent_control.ResumeLayout(false); * }*/ /// <summary> /// 保存字体设置信息到配置参数存储 /// </summary> public void SaveFontSetting() { if (this.MainForm != null && this.MainForm.AppInfo != null) { { // Create the FontConverter. System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(Font)); string strFontString = converter.ConvertToString(this.Font); this.MainForm.AppInfo.SetString( this.FormName, "default_font", strFontString); } { // Create the ColorConverter. System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(Color)); string strFontColor = converter.ConvertToString(this.ForeColor); MainForm.AppInfo.SetString( this.FormName, "default_font_color", strFontColor); } } }
/// <summary> /// This method retrieves a set of CDSAttribute from a class. /// </summary> /// <param name="data">The class to examine.</param> /// <param name="conv">A converter function to turn the value in to a string.</param> /// <returns>Returns an enumerable collection of attributes and values.</returns> public static IEnumerable<KeyValuePair<CDSAttributeAttribute, string>> CDSAttributesRetrieve( this IXimuraContent data, TypeConverter conv) { List<KeyValuePair<PropertyInfo, CDSAttributeAttribute>> attrList = AH.GetPropertyAttributes<CDSAttributeAttribute>(data.GetType()); foreach (KeyValuePair<PropertyInfo, CDSAttributeAttribute> reference in attrList) { PropertyInfo pi = reference.Key; if (pi.PropertyType == typeof(string)) { yield return new KeyValuePair<CDSAttributeAttribute, string>( reference.Value, pi.GetValue(data, null) as string); } else if (pi.PropertyType == typeof(IEnumerable<string>)) { IEnumerable<string> enumerator = pi.GetValue(data, null) as IEnumerable<string>; foreach (string value in enumerator) yield return new KeyValuePair<CDSAttributeAttribute, string>( reference.Value, value); } else if (conv != null && conv.CanConvertFrom(pi.PropertyType)) { yield return new KeyValuePair<CDSAttributeAttribute, string>( reference.Value, conv.ConvertToString(pi.GetValue(data, null))); } } }
/// <summary> /// Gets the localized description for given value and specified CultureInfo. /// </summary> /// <param name="enumValue">The given value.</param> /// <param name="ci">The CultureInfo.</param> /// <returns>The localized value.</returns> public static string GetLocalizedDescription(this Enum enumValue, CultureInfo ci) { if (enumValue != null) { System.ComponentModel.TypeConverter tc = TypeDescriptor.GetConverter(enumValue.GetType()); return(tc != null ? tc.ConvertToString(null, ci, enumValue) : enumValue.ToString()); } return(string.Empty); }
//</snippet1> // The following code example demonstrates how to use the // ColorConverter.ConvertToString method. This example // is designed to be used with Windows Forms. Paste this code // into a form and call the ShowColorConverter method when // handling the form's Paint event, passing e as PaintEventArgs. //<snippet2> private void ShowColorConverter(PaintEventArgs e) { Color myColor = Color.PaleVioletRed; // Create the ColorConverter. System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(myColor); string colorAsString = converter.ConvertToString(Color.PaleVioletRed); e.Graphics.DrawString(colorAsString, this.Font, Brushes.PaleVioletRed, 50.0F, 50.0F); }
//</snippet2> // The following code example demonstrates how to use the // ConvertToInvariantString and ConvertToString methods. // This example is designed to be used with Windows Forms. // Paste this code into a form and call the ShowFontStringConversion // method when handling the form's Paint event, passing e // as PaintEventArgs. //<snippet3> private void ShowFontStringConversion(PaintEventArgs e) { // Create the FontConverter. System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(Font)); Font font1 = (Font)converter.ConvertFromString("Arial, 12pt"); string fontName1 = converter.ConvertToInvariantString(font1); string fontName2 = converter.ConvertToString(font1); e.Graphics.DrawString(fontName1, font1, Brushes.Red, 10, 10); e.Graphics.DrawString(fontName2, font1, Brushes.Blue, 10, 30); }
public void test1() { Rectangle rect = new Rectangle(10, 12, 14, 16); // Wrong System.ComponentModel.TypeConverter baseConverter = new System.ComponentModel.TypeConverter(); string sample1 = baseConverter.ConvertToString(rect); // Right System.ComponentModel.TypeConverter rectSpecificConverter = TypeDescriptor.GetConverter(rect); string sample2 = rectSpecificConverter.ConvertToString(rect); log.DebugFormat("From new TypeConverter() {0}\r\n", sample1); log.DebugFormat("From TypeDescriptor.GetConverter() {0}\r\n", sample2); log.DebugFormat("From rect.ToString() {0}\r\n", rect.ToString()); }
public void test1() { Rectangle rect = new Rectangle(10, 12, 14, 16); // Wrong System.ComponentModel.TypeConverter baseConverter = new System.ComponentModel.TypeConverter(); string sample1 = baseConverter.ConvertToString(rect); // Right System.ComponentModel.TypeConverter rectSpecificConverter = TypeDescriptor.GetConverter(rect); string sample2 = rectSpecificConverter.ConvertToString(rect); log.DebugFormat("From new TypeConverter() {0}\r\n", sample1); log.DebugFormat("From TypeDescriptor.GetConverter() {0}\r\n", sample2); log.DebugFormat("From rect.ToString() {0}\r\n", rect.ToString()); }
// this and next method taken from // http://stackoverflow.com/questions/1940127/how-to-xmlserialize-system-drawing-font-class public static string FontToString(System.Drawing.Font font) { if (font == null) { return(null); } try { System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(System.Drawing.Font)); return(converter.ConvertToString(font)); } catch { return(null); } }
protected void OnMouseMove(object sender, MouseEventArgs e) { Point x = e.GetPosition(this); Matrix transform = Transform; foreach (Circuit.Terminal i in Symbol.Terminals) { Circuit.Coord tx = layout.MapTerminal(i); Point tp = new Point(tx.x, tx.y); tp = transform.Transform(tp); if ((tp - x).Length < 5.0) { ToolTip = "Terminal '" + i.ToString() + "'"; return; } } TextBlock text = new TextBlock(); Circuit.Component component = Symbol.Component; text.Inlines.Add(new Bold(new Run(component.ToString()))); foreach (PropertyInfo i in component.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(j => j.CustomAttribute <Circuit.Serialize>() != null && (j.CustomAttribute <BrowsableAttribute>() == null || j.CustomAttribute <BrowsableAttribute>().Browsable))) { object value = i.GetValue(component, null); DefaultValueAttribute def = i.CustomAttribute <DefaultValueAttribute>(); if (def == null || !Equals(def.Value, value)) { System.ComponentModel.TypeConverter tc = System.ComponentModel.TypeDescriptor.GetConverter(i.PropertyType); text.Inlines.Add(new Run("\n" + i.Name + " = ")); text.Inlines.Add(new Bold(new Run(tc.ConvertToString(value)))); } } ToolTip = new ToolTip() { Content = text }; }
private void button_ui_getDefaultFont_Click(object sender, EventArgs e) { Font font = null; if (String.IsNullOrEmpty(this.textBox_ui_defaultFont.Text) == false) { // Create the FontConverter. System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(Font)); font = (Font)converter.ConvertFromString(this.textBox_ui_defaultFont.Text); } else { font = Control.DefaultFont; } FontDialog dlg = new FontDialog(); dlg.ShowColor = false; dlg.Font = font; dlg.ShowApply = false; dlg.ShowHelp = true; dlg.AllowVerticalFonts = false; if (dlg.ShowDialog(this) != DialogResult.OK) { return; } { // Create the FontConverter. System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(Font)); this.textBox_ui_defaultFont.Text = converter.ConvertToString(dlg.Font); } }
public virtual void StoreSettings(XmlDocument intoDocument, XmlElement intoElement) { foreach (PropertyInfo pi in GetType().GetProperties()) { object valueObject = pi.GetValue(this); if (valueObject == null) { continue; } XmlElement propertyElement = intoDocument.CreateElement(pi.Name); intoElement.AppendChild(propertyElement); System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(pi.PropertyType); if (converter != null) { propertyElement.InnerText = converter.ConvertToString(valueObject); } else { propertyElement.InnerText = valueObject.ToString(); } } }
private ListViewItem CreateToolItem(NamedShellLink tool, TypeConverter keysConverter) { ListViewItem item = new ListViewItem(tool.Name) { Tag = tool }; ListViewItem.ListViewSubItem item2 = item.SubItems.Add(keysConverter.ConvertToString(tool.Hotkey)); if (tool.Hotkey == Keys.None) { item.UseItemStyleForSubItems = false; item2.ForeColor = SystemColors.GrayText; } return item; }
private String fontToString(Font font) { System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(Font)); return(converter.ConvertToString(font)); }
private static bool ConvertObjectToTypeInternal(object o, Type type, JavaScriptSerializer serializer, bool throwOnError, out object convertedObject) { IDictionary<string, object> dictionary = o as IDictionary<string, object>; if (dictionary != null) { return ConvertDictionaryToObject(dictionary, type, serializer, throwOnError, out convertedObject); } IList list = o as IList; if (list != null) { IList list2; if (ConvertListToObject(list, type, serializer, throwOnError, out list2)) { convertedObject = list2; return true; } convertedObject = null; return false; } if ((type == null) || (o.GetType() == type)) { convertedObject = o; return true; } //TypeDescriptor.GetConverter(type) !!! TypeConverter converter = new TypeConverter(); if (converter.CanConvertFrom(o.GetType())) { try { convertedObject = converter.ConvertFrom(null, CultureInfo.InvariantCulture, o); return true; } catch { if (throwOnError) { throw; } convertedObject = null; return false; } } if (converter.CanConvertFrom(typeof(string))) { try { string str; if (o is DateTime) { DateTime time = (DateTime)o; str = time.ToUniversalTime().ToString("u", CultureInfo.InvariantCulture); } else { //ConvertToInvariantString(o); !!! str = converter.ConvertToString(o); } //ConvertFromInvariantString(str); !!! convertedObject = converter.ConvertToString(str); return true; } catch { if (throwOnError) { throw; } convertedObject = null; return false; } } if (type.IsAssignableFrom(o.GetType())) { convertedObject = o; return true; } if (throwOnError) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, AtlasWeb.JSON_CannotConvertObjectToType, new object[] { o.GetType(), type })); } convertedObject = null; return false; }
private void UpdateCommandShortcut(ListViewItem item, Keys shortcutKeys, TypeConverter keysConverter) { ListViewItem.ListViewSubItem item2 = (item.SubItems.Count > 1) ? item.SubItems[1] : item.SubItems.Add("-"); if (keysConverter == null) { keysConverter = TypeDescriptor.GetConverter(typeof(Keys)); } item2.Text = keysConverter.ConvertToString(shortcutKeys); item2.ForeColor = SystemColors.GrayText; item.UseItemStyleForSubItems = shortcutKeys != Keys.None; }
/// <summary> /// This method retrieves a set of reference attributes from a class. /// </summary> /// <param name="data">The class to examine.</param> /// <param name="conv">A converter function to turn the value in to a string.</param> /// <returns>Returns an enumerable collection of attributes and values.</returns> public static IEnumerable<KeyValuePair<CDSReferenceAttribute, string>> CDSReferencesRetrieve( this IXimuraContent data, TypeConverter conv) { List<KeyValuePair<PropertyInfo, CDSReferenceAttribute>> attrList = AH.GetPropertyAttributes<CDSReferenceAttribute>(data.GetType()); foreach (KeyValuePair<PropertyInfo, CDSReferenceAttribute> reference in attrList) { PropertyInfo pi = reference.Key; if (pi.PropertyType != typeof(string) && (conv == null || !conv.CanConvertFrom(pi.PropertyType))) continue; string value; if (pi.PropertyType == typeof(string)) value = pi.GetValue(data, null) as string; else value = conv.ConvertToString(pi.GetValue(data, null)); yield return new KeyValuePair<CDSReferenceAttribute, string>(reference.Value, value); } }