none
Property Grid Custom Dropdown not behaving as I expected RRS feed

  • Question

  • I am writing an application that allows the user to create multiple instances of a particular object (as defined in the Widget class), set properties and then perform actions on the list of objects, in the order specified by one of the object properties. I am using a property grid to allow user editing of the selected objects properties at run time.

     

    The Widget class has a method GetAllowedPositions that returns a TypeConverter.StandardValuesCollection containing an array of strings {"1", "2", ... n} where n is the number of items in the list.

     

    The Order property of the object contains the execution order for that instance as an integer, and should be set to a number between 1 and the number of items in the list, and has the attribute [TypeConverter(typeof(OrderTypeConverter))].

     

    The OrderTypeConverter class is as follows:

     

    Code Block

    public class OrderTypeConverter : Int16Converter

    {

    public override bool GetStandardValuesSupported(ITypeDescriptorContext context)

    {

    return true;

    }

    public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)

    {

    return ((IAllowedPositions)context.Instance).GetAllowedPositions();

    }

    public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)

    {

    return true;

    }

    }

     

     

    This works fine in terms of the fact that it provides a drop down list that contains only the numbers that I want, and I can select any of those using the mouse quite hapily. In addition, if I type a digit outside of the allowed range nothing happens, which is ok, although I would have preferred a 'Value not Valid' message. However, if I type a digit within the allowed range, or double click on the entry in the property grid I get a 'Property Value is not Valid' message, with the detail message 'Object of type 'System.String' cannot be converted to type 'System.Int32'.'.

     

    I wanted to have a similar functionality to that with a Boolean type, or with one of the other generic types, in that if you double click on the cell then it cycles through the available options, and you can type an allowed value directly in to the cell if you wish.

     

    I have an idea that it must be something simple that I am missing, but I can't for the life of me see what. Anyone got any ideas?

     

    TIA for the help,

     

    Bernie

     

    Friday, November 9, 2007 12:32 PM

All replies

  •  

    Hi

    Based on my understanding, this error occurs because you override the GetStandardValuesExclusive method and always return true for that method. To achieve this, I would prefer the following way.

    Code Block

        class BusiObject

        {

            int orderType;

            [TypeConverter(typeof(OrderTypeConverter))]

            public int OrderType

            {

                get { return orderType; }

                set { orderType = value; }

            }

        }

     

        public class OrderTypeConverter : Int16Converter

        {

            public override bool GetStandardValuesSupported(ITypeDescriptorContext context)

            {

                return true;

            }

     

            public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)

            {

                return new StandardValuesCollection(new string[] { "1", "2", "3" });

            }

     

            public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)

            {

                System.Console.WriteLine("value is string");

                if (value is string)

                {

                    ArrayList _str = new ArrayList();

                    _str.AddRange(new string[] { "1", "2", "3" });

                   

                    string s = value.ToString();

                    if (_str.Contains(s))

                    {

                        Int16 i;

                        if (Int16.TryParse(s, out i))

                        {

                            return i;

                        }

                        else

                            throw new ArgumentException("Cannot convert '" + (string)value + "' to Int16");

                    }

                    else

                        throw new ArgumentException("There is no OrderType named '" + (string)value + "'");

                }

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

            }

    }

     

    Hope this helps.
    Best regards.
    Rong-Chun Zhang

    • Proposed as answer by Free.Cracker Wednesday, July 13, 2011 6:42 AM
    Thursday, November 15, 2007 11:56 AM
  • Hi Rong,

     

    Thanks for that - it kinda works, in that it now allows me to select values with a key press as well as with the drop down box. It still generates an error if I double click in the box, and it generates 2 messages (I changed the exception in the outer else case 'throw new ArgumentException("There is no OrderType named '" + (string)value + "'");' to a message box to save having to handle the exception) if I enter a number larger than the maximum allowed.

     

    Any ideas how to get round these issues?

     

    Bernie

    Thursday, November 15, 2007 2:41 PM
  • Thanks, it works like a charm
    Wednesday, July 13, 2011 6:42 AM