Пример #1
0
        /// <override></override>
        public override object CreateInstance(ITypeDescriptorContext context, System.Collections.IDictionary propertyValues)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (propertyValues == null)
            {
                throw new ArgumentNullException("propertyValues");
            }

            TextPadding txtPadding = (TextPadding)context.PropertyDescriptor.GetValue(context.Instance);
            TextPadding result     = TextPadding.Empty;
            int         all        = (int)propertyValues["All"];

            if (txtPadding.All != all)
            {
                result.All = all;
            }
            else
            {
                result.Left   = (int)propertyValues["Left"];
                result.Top    = (int)propertyValues["Top"];
                result.Right  = (int)propertyValues["Right"];
                result.Bottom = (int)propertyValues["Bottom"];
            }
            return(result);
        }
Пример #2
0
		/// <override></override>
		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
			TextPadding result = TextPadding.Empty;
			if (value is string) {
				string valueStr = value as string;
				if (valueStr == null) return base.ConvertFrom(context, culture, value);

				valueStr = valueStr.Trim();
				if (valueStr.Length == 0) return null;

				if (culture == null) culture = CultureInfo.CurrentCulture;
				char ch = culture.TextInfo.ListSeparator[0];
				string[] strArray = valueStr.Split(new char[] { ch });
				int[] numArray = new int[strArray.Length];
				TypeConverter converter = TypeDescriptor.GetConverter(typeof(int));
				for (int i = 0; i < numArray.Length; i++)
					numArray[i] = (int)converter.ConvertFromString(context, culture, strArray[i]);

				if (numArray.Length == 1)
					result.All = numArray[0];
				else if (numArray.Length == 4) {
					result.Left = numArray[0];
					result.Top = numArray[1];
					result.Right = numArray[2];
					result.Bottom = numArray[3];
				} else throw new ArgumentException();
			} else if (value is Padding) {
				Padding padding = (Padding)value;
				result.Left = padding.Left;
				result.Top = padding.Top;
				result.Right = padding.Right;
				result.Bottom = padding.Bottom;
			} else return base.ConvertFrom(context, culture, value);
			return result;
		}
Пример #3
0
        /// <override></override>
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == null)
            {
                throw new ArgumentNullException("destinationType");
            }
            if (value is TextPadding)
            {
                if (destinationType == typeof(string))
                {
                    TextPadding txtPadding = (TextPadding)value;
                    if (destinationType == typeof(string))
                    {
                        if (culture == null)
                        {
                            culture = CultureInfo.CurrentCulture;
                        }

                        string        separator = culture.TextInfo.ListSeparator + " ";
                        TypeConverter converter = TypeDescriptor.GetConverter(typeof(int));
                        string[]      strArray  = new string[4];
                        strArray[0] = converter.ConvertToString(context, culture, txtPadding.Left);
                        strArray[1] = converter.ConvertToString(context, culture, txtPadding.Top);
                        strArray[2] = converter.ConvertToString(context, culture, txtPadding.Right);
                        strArray[3] = converter.ConvertToString(context, culture, txtPadding.Bottom);
                        return(string.Join(separator, strArray));
                    }
                    if (destinationType == typeof(System.ComponentModel.Design.Serialization.InstanceDescriptor))
                    {
                        if (txtPadding.All < 0)
                        {
                            return(new System.ComponentModel.Design.Serialization.InstanceDescriptor(
                                       typeof(TextPadding).GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int), typeof(int) }),
                                       new object[] { txtPadding.Left, txtPadding.Top, txtPadding.Right, txtPadding.Bottom }));
                        }
                        else
                        {
                            return(new System.ComponentModel.Design.Serialization.InstanceDescriptor(
                                       typeof(TextPadding).GetConstructor(new Type[] { typeof(int) }), new object[] { txtPadding.All }
                                       ));
                        }
                    }
                }
                else if (destinationType == typeof(Padding))
                {
                    Padding paddingResult = Padding.Empty;
                    if (value != null)
                    {
                        TextPadding val = (TextPadding)value;
                        paddingResult.Left   = val.Left;
                        paddingResult.Top    = val.Top;
                        paddingResult.Right  = val.Right;
                        paddingResult.Bottom = val.Bottom;
                    }
                    return(paddingResult);
                }
            }
            return(base.ConvertTo(context, culture, value, destinationType));
        }
Пример #4
0
        public static string FixLength(this string text, int length, TextPadding padding = TextPadding.After)
        {
            if (text.Length >= length)
            {
                return(text.Substring(0, length));
            }

            switch (padding)
            {
            case TextPadding.After:
                string after = text.PadRight(length, '\u2002');
                return(after);

            case TextPadding.Before:
                string before = text.PadLeft(length, '\u2002');
                return(before);

            default:
                string def = text.PadRight(length);
                return(def);
            }
        }