Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PropertyGrid control and drop-down lists

Tags:

c#

winforms

I wanted to create a drop-down list as the editor for a property; If I had only strings as entries for the dropdown list, this would work fine (Using a StringConverter). However, when I tried to use a list of objects instead of Strings, this would not work (note how it works for normal comboboxes however!) This was my code:

    public static List<Bar> barlist;
    public Form1()
    {
        InitializeComponent();
        barlist = new List<Bar>();
        for (int i = 1; i < 10; i++)
        {
            Bar bar = new Bar();
            bar.barvalue = "BarObject " + i;
            barlist.Add(bar);
            comboBox1.Items.Add(bar);
        }
        Foo foo = new Foo();
        foo.bar = new Bar();
        propertyGrid1.SelectedObject = foo;
    }

    public class Foo
    {
        Bar mybar;

        [TypeConverter(typeof(BarConverter))]
        public Bar bar
        {
            get { return mybar; }
            set { mybar = value; }
        }
    }

    public class Bar
    {
        public String barvalue;
        public override String ToString()
        {
            return barvalue;
        }
    }


    class BarConverter : TypeConverter
    {
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            return new StandardValuesCollection(barlist);
        }
    }

The result (embedded into a form etc) looked like this:

enter image description here

Clicking an entry gave me this:

enter image description here

(Sorry about the german text, I am not sure if I can change that, my VS is english but my OS isnt, the error message is

  Invalid property value.

  The object of type "System.String" cannot be converted to type 
  "XMLTest.Form1+Bar".

I am pretty sure I can do a workaround for this by defining backwards conversion tools that convert a string back into a Bar object. This would require the keys being different in order to make this work properly. Is there a nicer way of doing this? Why does the comboBox that is embedded into the propertyGrid control use Strings (the normal comboBox has no problems whatsoever dealing with this)

On top of that, can I change the position of the middle separator programmatically? I have yet to find that option.

Thank you.

like image 241
CBenni Avatar asked Jan 29 '13 22:01

CBenni


1 Answers

I added the CanConvertFrom and ConvertFrom methods to your conversion class:

class BarConverter : TypeConverter
{
  public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
  {
    return true;
  }

  public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
  {
    return new StandardValuesCollection(barlist);
  }

  public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  {
    if (sourceType == typeof(string))
    {
      return true;
    }
    return base.CanConvertFrom(context, sourceType);
  }

  public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
  {
    if (value is string)
    {
      foreach (Bar b in barlist)
      {
        if (b.barvalue == (string)value)
        {
          return b;
        }
      }
    }
    return base.ConvertFrom(context, culture, value);
  }
}
like image 152
LarsTech Avatar answered Sep 29 '22 23:09

LarsTech