/// <summary> /// Converts the specified value to a <see cref="Size"/> /// </summary> /// <param name="context">Conversion context</param> /// <param name="culture">Culture to perform the conversion</param> /// <param name="value">Value to convert</param> /// <returns>A new instance of a <see cref="Size"/> converted from the specified <paramref name="value"/></returns> public override object ConvertFrom (ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { var text = value as string; if (text != null) { var parts = text.Split (culture.TextInfo.ListSeparator.ToCharArray ()); if (parts.Length != 2) throw new ArgumentException (string.Format (CultureInfo.CurrentCulture, "Cannot parse value '{0}' as size. Should be in the form of 'width,height'", text)); var converter = new Int32Converter (); return new Size ( (int)converter.ConvertFromString (context, culture, parts [0]), (int)converter.ConvertFromString (context, culture, parts [1]) ); } return base.ConvertFrom (context, culture, value); }
/// <summary> /// Converts the specified value to a <see cref="Rectangle"/> /// </summary> /// <param name="context">Conversion context</param> /// <param name="culture">Culture to perform the conversion</param> /// <param name="value">Value to convert</param> /// <returns>A new instance of a <see cref="Rectangle"/> converted from the specified <paramref name="value"/></returns> public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value) { string text = value as string; if (text != null) { var parts = text.Split (culture.TextInfo.ListSeparator.ToCharArray ()); if (parts.Length != 4) throw new ArgumentException (string.Format ("Cannot parse value '{0}'. Should be in the form of 'x, y, width, height'", text)); var converter = new Int32Converter (); return new Rectangle ( (int)converter.ConvertFromString (context, culture, parts [0]), (int)converter.ConvertFromString (context, culture, parts [1]), (int)converter.ConvertFromString (context, culture, parts [2]), (int)converter.ConvertFromString (context, culture, parts [3]) ); } return base.ConvertFrom (context, culture, value); }
/// <summary> /// Converts the specified value to a <see cref="Padding"/> object /// </summary> /// <param name="context">Conversion context</param> /// <param name="culture">Culture to perform the conversion for</param> /// <param name="value">Value to convert</param> /// <returns>A new instance of the <see cref="Padding"/> object with the value represented by <paramref name="value"/></returns> public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { string text = value as string; if (text != null) { var parts = text.Split(culture.TextInfo.ListSeparator.ToCharArray()); var converter = new Int32Converter(); switch (parts.Length) { case 1: return new Padding( (int)converter.ConvertFromString(context, culture, parts[0]) ); case 2: return new Padding( (int)converter.ConvertFromString(context, culture, parts[0]), (int)converter.ConvertFromString(context, culture, parts[1]) ); case 4: return new Padding( (int)converter.ConvertFromString(context, culture, parts[0]), (int)converter.ConvertFromString(context, culture, parts[1]), (int)converter.ConvertFromString(context, culture, parts[2]), (int)converter.ConvertFromString(context, culture, parts[3]) ); default: throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Cannot parse value '{0}'. Should be in the form of 'all', 'horizontal,vertical', or 'left, top, right, bottom'", text)); } } return base.ConvertFrom(context, culture, value); }
public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value) { string s = value as string; if (s == null) return base.ConvertFrom (context, culture, value); string [] subs = s.Split (culture.TextInfo.ListSeparator.ToCharArray ()); Int32Converter converter = new Int32Converter (); int[] numSubs = new int[subs.Length]; for (int i = 0; i < numSubs.Length; i++) { numSubs[i] = (int) converter.ConvertFromString (context, culture, subs[i]); } if (subs.Length != 4) throw new ArgumentException ("Failed to parse Text(" + s + ") expected text in the format \"x,y,Width,Height.\""); return new Rectangle (numSubs[0], numSubs[1], numSubs[2], numSubs[3]); }
private void button1_Click(object sender, System.EventArgs e) { Int32Converter converter = new Int32Converter(); UniformAlgorithm.DefaultLevelsR = (int)converter.ConvertFromString( textBox1.Text ); UniformAlgorithm.DefaultLevelsG = (int)converter.ConvertFromString( textBox2.Text ); UniformAlgorithm.DefaultLevelsB = (int)converter.ConvertFromString( textBox3.Text ); PopularityAlgorithm.DefaultNumColors = (int)converter.ConvertFromString( textBox4.Text ); OctreeAlgorithm.DefaultMaxColors = (int)converter.ConvertFromString( textBox5.Text ); OctreeAlgorithm.DefaultMaxColorBits = (int)converter.ConvertFromString( textBox6.Text ); ClusterAlgorithm.DefaultNumColors = (int)converter.ConvertFromString( textBox7.Text ); AdjustableAlgorithm.DefaultNumColors = (int)converter.ConvertFromString( textBox8.Text ); AdjustableAlgorithm.DefaultWP = (int)converter.ConvertFromString( textBox9.Text ); AdjustableAlgorithm.DefaultWD = (int)converter.ConvertFromString( textBox10.Text ); }