PropertyGrid - how to display a string array as dropdown property

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve

    PropertyGrid - how to display a string array as dropdown property


    I am using a property grid to display a classes properties, all is OK for
    single value properties but i need to display a list of strings for the user
    to select from and update the property value based on the selection.
    A Drop down list in the property grid would be ideal.
    For instance - like the StartPosition property of a form in the IDE.

    I think this would be a simple and often used procedure but i have not been
    able to locate an example of the code required, can anyone advise?



  • Marc Gravell

    #2
    Re: PropertyGrid - how to display a string array as dropdown property

    Normally this would be done via the type-converter of the property-type
    (string in this case), which you obviously can't change in this
    scenario. You could encapsulate the string into a simple class, and set
    the converter for that, otherwise just set the converter for the
    property:

    using System;
    using System.Componen tModel;
    using System.Windows. Forms;
    public class MyTest
    {
    private int test1;
    public int Test1
    {
    get { return test1; }
    set { test1 = value; }
    }
    private string test2;
    [TypeConverter(t ypeof(StringLis tConverter))]
    public string Test2
    {
    get { return test2; }
    set { test2 = value; }
    }
    }
    public class StringListConve rter : TypeConverter
    {
    public override bool
    GetStandardValu esSupported(ITy peDescriptorCon text context)
    {
    return true; // display drop
    }
    public override bool
    GetStandardValu esExclusive(ITy peDescriptorCon text context)
    {
    return true; // drop-down vs combo
    }
    public override StandardValuesC ollection
    GetStandardValu es(ITypeDescrip torContext context)
    {
    // note you can also look at context etc to build list
    return new StandardValuesC ollection(new string[] { "abc",
    "def", "ghi" });
    }
    }
    class Program
    {
    static void Main()
    {
    using (Form f = new Form())
    using(PropertyG rid pg = new PropertyGrid())
    {
    pg.Dock = DockStyle.Fill;
    pg.SelectedObje ct = new MyTest();
    f.Controls.Add( pg);
    Application.Run (f);
    }
    }
    }

    Comment

    • Steve

      #3
      Re: PropertyGrid - how to display a string array as dropdown property


      Thanks, works fine, I would like to extend my use of the Property grid
      to include objects within objects that would also be viewable/updateable
      within the one property grid.
      Can you point me in the direction of tutorial type documentation for
      property grid, the help doc explanations are difficult for me to grasp?

      rgds,Steve

      "Marc Gravell" <marc.gravell@g mail.comwrote in message
      news:1166161255 .607534.57330@7 9g2000cws.googl egroups.com...
      Normally this would be done via the type-converter of the property-type
      (string in this case), which you obviously can't change in this
      scenario. You could encapsulate the string into a simple class, and set
      the converter for that, otherwise just set the converter for the
      property:
      >
      using System;
      using System.Componen tModel;
      using System.Windows. Forms;
      public class MyTest
      {
      private int test1;
      public int Test1
      {
      get { return test1; }
      set { test1 = value; }
      }
      private string test2;
      [TypeConverter(t ypeof(StringLis tConverter))]
      public string Test2
      {
      get { return test2; }
      set { test2 = value; }
      }
      }
      public class StringListConve rter : TypeConverter
      {
      public override bool
      GetStandardValu esSupported(ITy peDescriptorCon text context)
      {
      return true; // display drop
      }
      public override bool
      GetStandardValu esExclusive(ITy peDescriptorCon text context)
      {
      return true; // drop-down vs combo
      }
      public override StandardValuesC ollection
      GetStandardValu es(ITypeDescrip torContext context)
      {
      // note you can also look at context etc to build list
      return new StandardValuesC ollection(new string[] { "abc",
      "def", "ghi" });
      }
      }
      class Program
      {
      static void Main()
      {
      using (Form f = new Form())
      using(PropertyG rid pg = new PropertyGrid())
      {
      pg.Dock = DockStyle.Fill;
      pg.SelectedObje ct = new MyTest();
      f.Controls.Add( pg);
      Application.Run (f);
      }
      }
      }
      >

      Comment

      • Marc Gravell

        #4
        Re: PropertyGrid - how to display a string array as dropdown property

        More example than tutorial; for what you want, the main thing is to
        specify something like ExpandableObjec tConverter as a type descriptor.
        However, much much more customisation is possible.

        Example:



        Marc

        Comment

        • Marc Gravell

          #5
          Re: PropertyGrid - how to display a string array as dropdown property

          descrpitor: I meant converter...

          "Marc Gravell" <marc.gravell@g mail.comwrote in message
          news:1166169715 .353340.105660@ t46g2000cwa.goo glegroups.com.. .
          More example than tutorial; for what you want, the main thing is to
          specify something like ExpandableObjec tConverter as a type
          descriptor.
          However, much much more customisation is possible.
          >
          Example:
          >

          >
          Marc
          >

          Comment

          Working...