سؤال

i have hashtable in videoformates class inherited from StringConvertor class. Two functions ConvertFrom and ConvertTo override there. How iplement these two functions to show video formates as string.

public class VideoFormate : StringConverter
{
    private Hashtable VideoFormates;
    public VideoFormate() {
            VideoFormates = new Hashtable();
            VideoFormates[".3g2"] = "3GPP2";
            VideoFormates[".3gp"] = "3GPP";
    }
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
    {
        return true;
    }

    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
        return new StandardValuesCollection(VideoFormates);
    }
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
    {
        return true;
    }
    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        return base.ConvertTo(context, culture, value, destinationType);
    }
    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        return base.ConvertFrom(context, culture, value);
    }

class for video properties is

class videoProperties
{
    private string _VideoFormat;

    [TypeConverter(typeof(VideoFormate)), 
    CategoryAttribute("Video Setting"),
    DefaultValueAttribute(0), 
    DescriptionAttribute("Select a Formate from the list")]
    public string VideoFormat
    {
        get { return _VideoFormat; }
        set { _VideoFormat = value; }
    }

}

i want to display 3GPP2 as display member and .3gp2 as value member in combo box in propertyGrid.

هل كانت مفيدة؟

المحلول

After debugging the code i found the answer.

On first run, in above code, propertyGrid dropdown fill with Hashtable collection. ConvertTo method convert it into string. so it save as System.Collections.DictionaryEntry. when select an element from combo box it set videoProperties.VideoFormat as System.Collections.DictionaryEntry. So i changed ConvertTo function as below.

public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        if (value is DictionaryEntry)
        {
            return ((DictionaryEntry)value).Key;
        }
        else if (value != null)
        {
            return VideoFormates[value];
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }

function check if value type is DictionaryEntry then it cast it and return the key element of value parameter. So combo box fill out with key values. when form shows ConvertTo function again called and second time return combo box values. then simplay return Hashtable value on the base of combo box value.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top