ConvertFromString() public method

public ConvertFromString ( ITypeDescriptorContext context, CultureInfo culture, string text ) : object
context ITypeDescriptorContext
culture System.Globalization.CultureInfo
text string
return object
示例#1
0
        void radListControl1_VisualItemFormatting(object sender, Telerik.WinControls.UI.VisualItemFormattingEventArgs args)
        {
            RadListVisualItem item = args.VisualItem;
            int rowIndex           = item.Data.RowIndex;

            System.ComponentModel.TypeConverter typeConverter = TypeDescriptor.GetConverter(typeof(Color));
            item.DrawFill      = true;
            item.BackColor     = (Color)typeConverter.ConvertFromString(null, CultureInfo.InvariantCulture, bgColors[rowIndex % 7]);
            item.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
        }
示例#2
0
 public static System.Drawing.Font StringToFont(string fontString)
 {
     try
     {
         System.ComponentModel.TypeConverter converter =
             System.ComponentModel.TypeDescriptor.GetConverter(typeof(System.Drawing.Font));
         return((System.Drawing.Font)converter.ConvertFromString(fontString));
     }
     catch
     {
         return(null);
     }
 }
示例#3
0
    // The following code example demonstrates how to use the
    // PointConverter.ConvertFromString and the Point.op_Subtraction
    // methods. This example is designed to be used with Windows
    // Forms. Paste this code into a form and call the
    // ShowPointConverter method when handling the form's Paint
    // event, passing e as PaintEventArgs.
    //<snippet1>
    private void ShowPointConverter(PaintEventArgs e)
    {
        // Create the PointConverter.
        System.ComponentModel.TypeConverter converter =
            System.ComponentModel.TypeDescriptor.GetConverter(typeof(Point));

        Point point1 = (Point)converter.ConvertFromString("200, 200");

        // Use the subtraction operator to get a second point.
        Point point2 = point1 - new Size(190, 190);

        // Draw a line between the two points.
        e.Graphics.DrawLine(Pens.Black, point1, point2);
    }
示例#4
0
    //</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);
    }
示例#5
0
 private static object ParseProperty( PropertyInfo property, string valueString, TypeConverter converter )
 {
     try
     {
         return converter.ConvertFromString( valueString );
     }
     catch ( Exception ex )
     {
         throw new PropertyParsingException(
             property,
             valueString,
             String.Format( _parsingErrorFormat, property.DeclaringType.Name, property.Name, property.PropertyType.Name, valueString ), ex );
     }
 }
示例#6
0
文件: MyForm.cs 项目: gvhung/dp2
        /// <summary>
        /// 从配置参数存储中装载字体设置信息
        /// </summary>
        public void LoadFontSetting()
        {
            if (this.MainForm == null)
            {
                return;
            }

            if (this.MainForm != null && this.MainForm.AppInfo != null)
            {
                string strFontString = MainForm.AppInfo.GetString(
                    this.FormName,
                    "default_font",
                    "");  // "Arial Unicode MS, 12pt"

                if (String.IsNullOrEmpty(strFontString) == false)
                {
                    // Create the FontConverter.
                    System.ComponentModel.TypeConverter converter =
                        System.ComponentModel.TypeDescriptor.GetConverter(typeof(Font));

                    this.Font = (Font)converter.ConvertFromString(strFontString);
                }
                else
                {
                    // 沿用系统的缺省字体
                    if (this.MainForm != null)
                    {
                        MainForm.SetControlFont(this, this.MainForm.DefaultFont);
                    }
                }

                string strFontColor = MainForm.AppInfo.GetString(
                    this.FormName,
                    "default_font_color",
                    "");

                if (String.IsNullOrEmpty(strFontColor) == false)
                {
                    // Create the ColorConverter.
                    System.ComponentModel.TypeConverter converter =
                        System.ComponentModel.TypeDescriptor.GetConverter(typeof(Color));

                    this.ForeColor = (Color)converter.ConvertFromString(strFontColor);
                }
                this.PerformLayout();
            }
        }
示例#7
0
            private static void Label_set(Label l, Color c, int x, int y, int w, int h)
            {
                System.ComponentModel.TypeConverter converter =
                    System.ComponentModel.TypeDescriptor.GetConverter(typeof(Font));
                Font f = (Font)converter.ConvertFromString("Cambria, 18pt, style=Bold, Italic");

                l.Font      = f;
                l.BackColor = Color.Black;
                l.Visible   = false;

                l.ForeColor = c;
                l.Top       = x;
                l.Left      = y;

                l.Width  = w;
                l.Height = h;
            }
 private static void setPropertyValue(PropertyInfo pi, Type propertyType, object oObj, string value)
 {
     if (propertyType == typeof(string))
     {
         pi.SetValue(oObj, value, null);
         return;
     }
     object[] attributes = propertyType.GetCustomAttributes(typeof(System.ComponentModel.TypeConverterAttribute), false);
     foreach (System.ComponentModel.TypeConverterAttribute converterAttribute in attributes)
     {
         System.ComponentModel.TypeConverter converter = (System.ComponentModel.TypeConverter)Activator.CreateInstance(Type.GetType(converterAttribute.ConverterTypeName));
         if (converter.CanConvertFrom(value.GetType()))
         {
             // good - use the converter to restore the value
             pi.SetValue(oObj, converter.ConvertFromString(value), null);
             break;
         }
     }
 }
示例#9
0
 public virtual void RestoreSettings(XmlElement element)
 {
     foreach (PropertyInfo pi in GetType().GetProperties())
     {
         // Should be only one
         foreach (XmlElement propertyElem in element.GetElementsByTagName(pi.Name))
         {
             System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(pi.PropertyType);
             if (converter != null)
             {
                 try
                 {
                     pi.SetValue(this, converter.ConvertFromString(propertyElem.InnerText));
                 } catch (Exception)
                 {
                 }
             }
         }
     }
 }
示例#10
0
        /// <summary>
        /// Handles &lt;color=value&gt;
        /// </summary>
        /// <param name="tag"></param>
        /// <param name="oldColor"></param>
        /// <returns></returns>
        private static Color ParseColor(string tag, Color?oldColor)
        {
            Color result = Control.DefaultForeColor;

            if (oldColor.HasValue)
            {
                result = oldColor.Value;
            }

            try
            {
                result = (Color)typeConverter.ConvertFromString(null, CultureInfo.InvariantCulture, tag);
            }
            catch (Exception)
            {
                //failed to convert color
            }

            return(result);
        }
示例#11
0
        public DefaultValueAttribute(Type type, string value)
        {
            try {
#if NET_2_1
                if (type.IsEnum)
                {
                    DefaultValue = Enum.Parse(type, value);
                }
                else if (type == typeof(TimeSpan))
                {
                    DefaultValue = TimeSpan.Parse(value);
                }
                else
                {
                    DefaultValue = Convert.ChangeType(value, type, null);
                }
#else
                TypeConverter converter = TypeDescriptor.GetConverter(type);
                DefaultValue = converter.ConvertFromString(null, CultureInfo.InvariantCulture, value);
#endif
            } catch { }
        }
示例#12
0
        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);
            }
        }
示例#13
0
		Func <object, bool> SetupComparer ()
		{
			Type ot = OperandType;

			object min = Minimum, max = Maximum;
#if NET_4_0
			if (min == null || max == null)
				throw new InvalidOperationException ("The minimum and maximum values must be set.");
#endif
			if (min is int)
				return new Func <object, bool> (CompareInt);

			if (min is double)
				return new Func <object, bool> (CompareDouble);
			
			if (ot == null)
				throw new InvalidOperationException ("The OperandType must be set when strings are used for minimum and maximum values.");
			
			if (!typeof(IComparable).IsAssignableFrom (ot)) {
#if NET_4_0
				string message = String.Format ("The type {0} must implement System.IComparable", ot.FullName);
				throw new InvalidOperationException (message);
#else
				throw new ArgumentException ("object");
#endif
			}
			
			string smin = min as string, smax = max as string;
			cvt = TypeDescriptor.GetConverter (ot);
			Minimum = cvt.ConvertFromString (smin);
			Maximum = cvt.ConvertFromString (smax);

			return new Func <object, bool> (CompareArbitrary);
		}
 private void LoadFontSettings()
 {
     System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(Font));
     this.Font = (Font)converter.ConvertFromString(ConfigurationManager.AppSettings["fontSettings"]);
 }
示例#15
0
 public static Keys ConvertCharToVirtualKey(string key)
 {
     System.ComponentModel.TypeConverter converter = TypeDescriptor.GetConverter(typeof(Keys));
     return((Keys)converter.ConvertFromString(key));
 }
示例#16
0
 private Font stringToFont(String font)
 {
     System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(Font));
     return((Font)converter.ConvertFromString(font));
 }