NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Type Converter Sample

The complete source code for a Type Converter for the FlashTrackBar sample in the Win Forms Control Sample is given below. To download the source code, see the Win Forms Quickstart.

namespace SampleControlPack {

    using System;
    using System.ComponentModel;
    //
    // <doc>
    // <desc>
    // This is a type converter for the flash track bar value property.
    // It demonstrates a typical scenario -- accepting input in
    // multiple formats and converting it to the desired type.  
    //It accepts a decimal or integer number for the value property,
    // and also a percentage.
    // </desc>
    // </doc>
    //
    public class FlashTrackBarValueConverter : TypeConverter {
        
        //
        // <doc>
        // <desc>
        // Override this method to indicate that 
        // FlashTrackBarValueConverter can convert from
        // string data types. In this case, it is not really
        // necessary to override CanConvertTo
        // because, by default, all types can be converted to string.
        // </desc>
        // </doc>
        //
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
            if (sourceType == typeof(string)) {
                return true;
            }
            return base.CanConvertFrom(context, sourceType);
        }

        //
        // <doc>
        // <desc>
        //Does the actual conversino from string to value. You will 
        //support an optional percentage value
        //here, and also attempt to constrain the value to the min and 
        //max values of the control.
        // </desc>
        // </doc>
        //
        public override object ConvertFrom(ITypeDescriptorContext context, object value, object[] arguments) {
            
            if (value is string) {
                int convertedValue;
                string text = ((string)value).Trim();
                
                // Does the value end in a percentage?
                //
                if (text.EndsWith("%")) {
                    text = text.Substring(0, text.Length - 1).Trim();
                    float percent = float.Parse(text);
                    
                    // Ok, you have a percentage.  But, a percentage of  
                    // what? To find that out you need to get at the                      
                    // context object and see if you
                    // can find the flash track bar we are editing.
                    if (context != null && context.Instance is  
                    FlashTrackBar) {
                        FlashTrackBar t = (FlashTrackBar)context.Instance;
                        convertedValue = (int)((float)(t.Max - t.Min) * 
                        percent) / 100 + t.Min;
                    }
                    else {
                        // Otherwise, fall back to just assigning the                          
                        // value out.
                        convertedValue = (int)percent;
                    }
                }
                else {
                    convertedValue = int.Parse(text);
                }
                
                // Now that you have the converted value, constrain it by 
                // the min and max properties of the control.  
                // Notice that here, as in above, you always
                // check that context has what you are looking for.  The 
                // context parameter that is passed to this method is 
                // entirely optional, as are all of the properties on the
                // context object.  Because of this, you must check
                // everything and be prepared to 
                // fall back to less-specific behavior.

                if (context != null && context.Instance is FlashTrackBar)    
                {
                    FlashTrackBar t = (FlashTrackBar)context.Instance;
                    convertedValue = Math.Max(convertedValue, t.Min);
                    convertedValue = Math.Min(convertedValue, t.Max);
                }
                
                return convertedValue;
            }
            
            return base.ConvertFrom(context, value, arguments);
        }
    }
}