コード例 #1
0
ファイル: PaddingConverter.cs プロジェクト: sven82/winforms
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string valueStr)
            {
                valueStr = valueStr.Trim();
                if (valueStr.Length == 0)
                {
                    return(null);
                }

                // Parse 4 integer values.
                if (culture == null)
                {
                    culture = CultureInfo.CurrentCulture;
                }

                char          sep          = culture.TextInfo.ListSeparator[0];
                string[]      tokens       = valueStr.Split(new char[] { sep });
                int[]         values       = new int[tokens.Length];
                TypeConverter intConverter = TypeDescriptor.GetConverter(typeof(int));
                for (int i = 0; i < values.Length; i++)
                {
                    // Note: ConvertFromString will raise exception if value cannot be converted.
                    values[i] = (int)intConverter.ConvertFromString(context, culture, tokens[i]);
                }

                if (values.Length != 4)
                {
                    throw new ArgumentException(string.Format(SR.TextParseFailedFormat,
                                                              nameof(value),
                                                              valueStr,
                                                              "left, top, right, bottom"));
                }

                return(new Padding(values[0], values[1], values[2], values[3]));
            }

            return(base.ConvertFrom(context, culture, value));
        }
コード例 #2
0
        /// <summary>
        ///  Converts the given object to the converter's native type.
        /// </summary>
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string valueStr)
            {
                string text = valueStr.Trim();
                if (text.Length == 0)
                {
                    return(null);
                }

                // Parse 2 integer values.
                if (culture == null)
                {
                    culture = CultureInfo.CurrentCulture;
                }

                char          sep          = culture.TextInfo.ListSeparator[0];
                string[]      tokens       = text.Split(new char[] { sep });
                int[]         values       = new int[tokens.Length];
                TypeConverter intConverter = TypeDescriptor.GetConverter(typeof(int));
                for (int i = 0; i < values.Length; i++)
                {
                    values[i] = (int)intConverter.ConvertFromString(context, culture, tokens[i]);
                }

                if (values.Length != 2)
                {
                    throw new ArgumentException(string.Format(SR.TextParseFailedFormat,
                                                              text,
                                                              "Start, Length"));
                }

                return(new LinkLabel.Link(values[0], values[1]));
            }

            return(base.ConvertFrom(context, culture, value));
        }