internal static object INTERNAL_ConvertFromString(string cursorStr) { if (Enum.TryParse(cursorStr, out CursorType cursorType) && IsValidCursorType(cursorType)) { return(Cursors.EnsureCursor(cursorType)); } else { throw new ArgumentException(string.Format("'{0}' cursor type is not valid.", cursorStr)); } }
/// <summary> /// TypeConverter method implementation. /// </summary> /// <param name="context">ITypeDescriptorContext</param> /// <param name="culture">current culture (see CLR specs)</param> /// <param name="value">value to convert from</param> /// <returns>value that is result of conversion</returns> public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string s) { string text = s.Trim(); if (Enum.TryParse(text, true, out CursorType cursorType) && (int)cursorType >= (int)CursorType.None && (int)cursorType <= (int)CursorType.Eraser) { return(Cursors.EnsureCursor(cursorType)); } else { throw new FormatException($"'{value}' is not a valid token."); } } throw GetConvertFromException(value); }
static Cursor() { TypeFromStringConverters.RegisterConverter(typeof(Cursor), INTERNAL_ConvertFromString); Cursors.FillCursorTypeToStringDictionary(); }