locked
UITypeEditor for System.Type RRS feed

  • Question

  • I have a property DataType which contains a value of System.Type.  I want to have a UITypeEditor that brings up a dropdown of all the System.Types.  I've got as far as creating a "DataTypeEditor" which inherits from UITypeEditor and GetEditStyle returns UiEditorEditStyle.DropDown.  I know I need to override EditValue, but I am not sure how to create an editor control ot display all the System.Type values.  Can anyone point me in a direction?  Thanks.
    Friday, September 7, 2007 12:28 PM

Answers

  •  

    Hi LiteWait,

    We can get all types of current project with reflection. Try the following code:

    Code Snippet

    using System.Reflection;

    namespace FormP

    {

        public partial class Form10 : Form

        {

            public Form10()

            {

                InitializeComponent();

            }

     

            private void button1_Click(object sender, EventArgs e)

            {

                Assembly[] array = AppDomain.CurrentDomain.GetAssemblies();

                foreach (Assembly a in array)

                {

                    this.treeView1.Nodes.Add(a.GetName().Name,a.GetName().Name);

                    Type[] types = a.GetTypes();

                    foreach (Type t in types)

                    {

                        if (t.IsPublic)

                            this.treeView1.Nodes[a.GetName().Name].Nodes.Add(t.FullName);

                    }

                }

                treeView1.ExpandAll();

            }

        }

    }

     

    You can create a UserControl with a TteeView in it. The TreeView is used to show all valid types. And then make the UserControl as the editor control of your ‘DataTypeEditor’.

    Best regards.
    Rong-Chun Zhang

    Thursday, September 13, 2007 9:00 AM

All replies

  •  

    Hi LiteWait,

    We can get all types of current project with reflection. Try the following code:

    Code Snippet

    using System.Reflection;

    namespace FormP

    {

        public partial class Form10 : Form

        {

            public Form10()

            {

                InitializeComponent();

            }

     

            private void button1_Click(object sender, EventArgs e)

            {

                Assembly[] array = AppDomain.CurrentDomain.GetAssemblies();

                foreach (Assembly a in array)

                {

                    this.treeView1.Nodes.Add(a.GetName().Name,a.GetName().Name);

                    Type[] types = a.GetTypes();

                    foreach (Type t in types)

                    {

                        if (t.IsPublic)

                            this.treeView1.Nodes[a.GetName().Name].Nodes.Add(t.FullName);

                    }

                }

                treeView1.ExpandAll();

            }

        }

    }

     

    You can create a UserControl with a TteeView in it. The TreeView is used to show all valid types. And then make the UserControl as the editor control of your ‘DataTypeEditor’.

    Best regards.
    Rong-Chun Zhang

    Thursday, September 13, 2007 9:00 AM
  • There is no need to use a type editor as a type converter will suffice:

     

    using System;

    using System.Reflection;

    using System.Globalization;

    using System.ComponentModel;

    using System.Collections.Generic;

     

    public class DataTypeConverter : TypeConverter

    {

       public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)

       {

          bool canConvertTo = false;

     

          if (destinationType == typeof(string))

          {

             canConvertTo = true;

          }

          else

          {

             canConvertTo = base.CanConvertTo(context, destinationType);

          }

     

          return canConvertTo;

       }

     

       public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)

       {

          object convertedValue = null;

          Type instance = null;

     

          if (value is Type)

          {

             instance = (Type)value;

          }

     

          if (destinationType == typeof(string))

          {

             if (value is string)

             {

                convertedValue = value;

             }

             else if (instance != null)

             {

                convertedValue = instance.Name;

             }

          }

          else

          {

             convertedValue = base.ConvertTo(context, culture, value, destinationType);

          }

     

          return convertedValue;

       }

     

       public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)

       {

          bool canConvertFrom = false;

     

          if (sourceType == typeof(string))

          {

             canConvertFrom = true;

          }

          else

          {

             canConvertFrom = base.CanConvertFrom(context, sourceType);

          }

     

          return canConvertFrom;

       }

     

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

       {

          object convertedValue = null;

     

          if (value is string)

          {

             // all types represented by TypeCode are in the System namespace.

             convertedValue = Type.GetType("System." + value);

          }

          else

          {

             convertedValue = base.ConvertFrom(context, culture, value);

          }

     

          return convertedValue;

       }

     

       public override bool GetStandardValuesSupported (ITypeDescriptorContext context)

       {

          return true;

       }

     

       public override TypeConverter.StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)

       {

          List<string> typeList = new List<string>(Enum.GetNames(typeof(TypeCode)));

     

          // remove the two "non-standard" types.

          typeList.Remove("DBNull");

          typeList.Remove("Empty");

          string[] typeArray = typeList.ToArray();

          Array.Sort(typeArray);

          return new TypeConverter.StandardValuesCollection(typeArray);

       }

     

       public override bool GetStandardValuesExclusive (ITypeDescriptorContext context)

       {

          // return true here if you want to not allow the

          // user to enter a value that is not in the list.

          return false;

       }

    }

    Monday, September 17, 2007 5:32 PM