我自己定义一个控件 现在需要加入这样的属性,这样做的是方便开发者输入方便。
可是在添加的时候提示,未找到类型System.String上的构造函数。 改为List<int> 的不抱错,是值类型和引用类型的区别。
     private List<string > amountColumnPropertyName=new List<string > ();
        [Description("显示小计行的列"), Category("小计")]
        /// <summary>
        /// 显示小计行的列
        /// </summary>
        public List<string > AmountColumnPropertyName
        {
            get { return amountColumnPropertyName; }
            set { amountColumnPropertyName = value; }
        }
我该怎么弄,才能正常让开发者输入, 请不要说String[] 的类型,这样的每一次都要换行,说不符合。求教高手,谢谢了。

解决方案 »

  1.   

    你中间有空格吧 List<string> 没问题。
      

  2.   

     
    我已经改为这样的了,还是一样。两位大牛你们应该用过吧。可以简单测试下啊。       private List<string> amountColumnPropertyName=new List<string>();
            [Description("显示小计行的列"), Category("小计")]
            /// <summary>
            /// 显示小计行的列
            /// </summary>
            public List<string> AmountColumnPropertyName
            {
                get { return amountColumnPropertyName; }
                set { amountColumnPropertyName = value; }
            }
     
      

  3.   

    [Browsable(true)]
    [Description("显示小计行的列"), Category("小计")]
    public List<string> TestList{ get; set; }
    没问题
      

  4.   

    完整代码;public class TestPro : Control
        {
            public TestPro()
            {
            }
            private List<string> amountColumnPropertyName = new List<string>();
            [Description("显示小计行的列"), Category("小计")]
            /// <summary>
            /// 显示小计行的列
            /// </summary>
            public List<string> AmountColumnPropertyName
            {
                get { return amountColumnPropertyName; }
                set { amountColumnPropertyName = value; }
            }
    我刚刚写的也不行,和上面一样的问题,系统win7,vs2010,我纳闷了。
      

  5.   

    你这贴的不全吧?连最后那个{都没有,以下测试,没有问题    public class TestPro : Control
        {
            public TestPro()
            {
            }
            private List<string> amountColumnPropertyName = new List<string>();
            [Description("显示小计行的列"), Category("小计")]
            /// <summary>
            /// 显示小计行的列
            /// </summary>
            public List<string> AmountColumnPropertyName
            {
                get { return amountColumnPropertyName; }
                set { amountColumnPropertyName = value; }
            }
        }
      

  6.   

    不会吧,我的为什么不能呢?所有代码 
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.ComponentModel;namespace MyTest.TestControl
    {
        public class TestPro : Control
        {
            public TestPro()
            {
            }
            private List<string> amountColumnPropertyName = new List<string>();
            [Description("显示小计行的列"), Category("小计")]
            /// <summary>
            /// 显示小计行的列
            /// </summary>
            public List<string> AmountColumnPropertyName
            {
                get { return amountColumnPropertyName; }
                set { amountColumnPropertyName = value; }
            }        [Browsable(true)]
            [Description("显示小计行的列"), Category("小计")]
            public List<string> TestList { get; set; }    }
    }
      

  7.   

    vs2008 测试没问题using System.Collections.Generic;
    using System.Windows.Forms;
    using System.ComponentModel;namespace WindowsFormsApplication1
    {
        class TestPro : Control
        {
            public TestPro()
            {
            }
            private List<string> amountColumnPropertyName = new List<string>();
            [Description("显示小计行的列"), Category("小计")]
            /// <summary>
            /// 显示小计行的列
            /// </summary>
            public List<string> AmountColumnPropertyName
            {
                get { return amountColumnPropertyName; }
                set { amountColumnPropertyName = value; }
            }
        }
    }
      

  8.   

    我的是vs2010 ,win7系统,我找三个同事电脑测试,都有问题。都是vs2010
      

  9.   

    估计又是 版本的问题 比如
    .net2 适应2008
    .net4 2010
      

  10.   


    Control继承有问题,你继承Component或UserControl就木有问题,刚测过
      

  11.   

    刚刚帮你测试了下,确实报错
    Win7 64位,VS2010
    正在试解决方案
      

  12.   

    应该是String类型的没有默认构造函数的原因可以从两个反面解决,一重写UITypeEidtor;二更改数据类型,比如,自己包装一个类型
      

  13.   

    xp,VS2010没问题
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.ComponentModel;namespace ClassLibrary1
    {
        public class TestPro : Control
        {
            public TestPro()
            {        }
            private List<string> amountColumnPropertyName = new List<string>();
            [Description("显示小计行的列"), Category("小计")]
            /// <summary>
            /// 显示小计行的列
            /// </summary>
            public List<string> AmountColumnPropertyName
            {
                get { return amountColumnPropertyName; }
                set { amountColumnPropertyName = value; }
            }        [Browsable(true)]
            [Description("显示小计行的列"), Category("小计")]
            public List<string> TestList { get; set; }    }
    }
      

  14.   

    这样写就不会报错了
    public class MyStr
    {
        private string str = "";    public string 字符串
        {
            get { return str; }
            set { str = value; }
        }
    }public class TagTest
    {
        private List<MyStr> amountColumnPropertyName = new List<MyStr>();    [Description("显示小计行的列"), Category("小计")]
        public List<MyStr> AmountColumnPropertyName
        {
            get { return amountColumnPropertyName; }
            set { amountColumnPropertyName = value; }
        }
    }propertyGrid1.SelectedObject = new TagTest();
      

  15.   

    三种方法的代码。都放到一起了。using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.ComponentModel;namespace MyTest.TestControl
    {
        //Component拖到界面上显示在下面
        public class TestPro : Control
        {
            public TestPro()
            {
            }        private List<string> amountColumnPropertyName = new List<string>();
            [Description("显示小计行的列"), Category("小计"),Browsable(true)]
            /// <summary>
            /// 显示小计行的列
            /// </summary>
            public List<string> AmountColumnPropertyName  //报错,未能找到System.String上的构造函数
            {
                get { return amountColumnPropertyName; }
                set { amountColumnPropertyName = value; }
            }        private List<AmountColumn> amountColumnList = new List<AmountColumn>();        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible ), Description("显示小计行的列"), Category("小计"), Browsable(true)]
            public List<AmountColumn> AmountColumnList//自己写的一个 问题是序列问题
            {
                get { return amountColumnList; }
                set { amountColumnList = value; }
            }
            /// <summary>
            /// 
            /// </summary>
            public string[] AmountColumnArray    //这样没有问题可是用户输入不方便,见面设计不符合要求
            {
                get;
                set;
            }
        }    [Serializable]
        public class AmountColumn
        {
            public AmountColumn()
            {
            }
            string propertyName;
            [Description("要显示小计的列"), Browsable(true)]
            public string PropertyName
            {
                get { return propertyName; }
                set { propertyName = value; }
            }
            
        }}
      

  16.   

    说实话,方法有了,其他都可以完善,自己多思考下呀序列化+显示内容+恢复你的List<string>解决方法:
    [Serializable]
    public class MyStr
    {
        private string str = "";    public string 字符串
        {
            get { return str; }
            set { str = value; }
        }    public override string ToString()
        {
            return str;
        }
    }public class TagTest
    {
        private List<MyStr> amountColumnPropertyName = new List<MyStr>();    [Browsable(false)]
        public List<string> AmountColumnPropertyNameList
        {
            get { return (from p in amountColumnPropertyName select p.字符串).ToList(); }
        }    [Description("显示小计行的列"), Category("小计")]
        public List<MyStr> AmountColumnPropertyName
        {
            get { return amountColumnPropertyName; }
            set { amountColumnPropertyName = value; }
        }
    }
      

  17.   

       [Category("UserName"),
             Description("组合控件中label的文本显示属性")]
            public string UserName
            {
                get
                {
                    return label3.Text;
                }
                set
                {
                    label3.Text = value;
                }
            }
    我是这样封装的  你可以参考
      

  18.   

    [Description("显示小计行的列"), Category("小计")]
      public List<MyStr> AmountColumnPropertyName
      {
      get { return amountColumnPropertyName; }
      set { amountColumnPropertyName = value; }
      }
    这样的写能行吗? 我的一直这样的错误。 才去加  [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible )]不然的话这样的错误
    未能找到具有某个名称的类型。类型名称为“System.Collections.Generic.List`1[[MyTest.TestControl.AmountColumn, MyTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”。  
      

  19.   

    楼主,把文档注释和attribute换个位置试试!
      

  20.   


    /******************************************
    -- Author       : Coder.Yan
    -- Create date  : 2011-09-21
    -- Description  : 
     ******************************************/
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    using System.Drawing;
    using System.Drawing.Design;namespace WindowsFormsApplication2
    {
        public class Control1:System.Windows.Forms.Control
        {
            private List<string> _list = new List<string>();        [Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
            public List<string> List
            {
                get { return _list; }
                set { _list = value; }
            }
        }
    }VS2010 Win7测试通过
      

  21.   

    我 win7, vs2010 没问题啊
      

  22.   

    Sorry , 我看错了,确实报错
      

  23.   


    这个应该正解, 估计楼上好多py和我一样,以为是添加控件时报的错,其实lz说的是,编辑控件属性时,点击Add按钮报的错。
      

  24.   

    这样的和String[]属性没有区别了,我要的就是点击属性后和那个List<int>的界面编辑器效果。
      

  25.   

    自己写个UITypeEditor啊,MSDN上有列子,可以弹出一个自定义界面的。
      

  26.   

    你有试过自定义一个UITypeEditor?
    简单的就用 ListControlStringCollectionEditor,如果实在想要,左边列表右边属性,可能自定义一个Form,让Eidtor弹出,默认添加的时候,添加一个String.Empty,而不是new string()
      

  27.   

    虽然设计时支持一般的开发很少用到,不过,既然你要实现VS中默认无法实现的效果,MSDN这方面的东西肯定是要看的
      

  28.   

    lz没戏了?  建议你初始化时先生成一定数量的string值放到list<string>中看看可以不
      

  29.   

    要是给出类似的代码就好了,就是List<int>的效果。