1. 程式人生 > >C# PropertyGrid控制元件的四個自定義操作

C# PropertyGrid控制元件的四個自定義操作

1>PropertyGid 控制元件輸入時顯示隱藏密碼為(*)

單獨寫一個PasswordStringConverter 類;

using System.ComponentModel;
using System.Globalization;

namespace Test
{
    public class PasswordStringConverter : StringConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return
base.CanConvertFrom(context, sourceType); } public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if (destinationType.GetType() == typeof(string)) return true; return base.CanConvertTo(context, destinationType); } public
override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (value.GetType() == typeof(string)) { int stringSize; string retVal = "* "; Random randomString = new
Random(); if (value != null) stringSize = ((string)value).Length; else stringSize = randomString.Next(10); for (int i = 0; i < stringSize; i++) retVal += "* "; return retVal; } return base.ConvertTo(context, culture, value, destinationType); } public override bool GetPropertiesSupported(ITypeDescriptorContext context) { return false; } public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { string[] standardValues = new string[1]; int stringSize; string retVal = "* "; Random randomString = new Random(); stringSize = randomString.Next(10); for (int i = 0; i < stringSize; i++) retVal += "* "; standardValues[0] = retVal; return new StandardValuesCollection(standardValues); } } }

呼叫;

        private string _pwd;

        [CategoryAttribute("資料庫連線設定"),
         DisplayNameAttribute("密碼"),
         DescriptionAttribute("連線資料庫的密碼"),
         TypeConverter(typeof(PasswordStringConverter)),  //關鍵(過載)
        ]
        public string Pwd
        {
            get { return _pwd; }
            set { _pwd = value; }
        }

效果:

這裡寫圖片描述

2>PropertyGid 控制元件選擇下拉框值

單獨寫一個SetTimeItem 類; //@! SetTimeItem 隨便建

using System.ComponentModel;

namespace Test
{
    public class SetTimeItem : StringConverter
    {
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            //@! 編輯下拉框中的items
            return new StandardValuesCollection(new string[] { "30", "60" ,"180" }); 
        }
        //@! true: disable text editting.    false: enable text editting;
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return true;
        }
    }
}

呼叫;

  private string _SetTime;

  [DisplayNameAttribute("時隔時間設定"),
  CategoryAttribute("時間設定"),
  DescriptionAttribute("設定讀取服務時隔時間"),
  TypeConverter(typeof(SetTimeItem)),
  ]
  public string SetTime
  {
   get { return _SetTime; }
   set { _SetTime = value; }
  }

效果;

這裡寫圖片描述

3>PropertyGid 控制元件排序

(如果不排序,它會自動按照拼音首字母給你排序,排版變得比較醜)

單獨寫一個PropertySorter類;

using System;
using System.Collections;
using System.ComponentModel;

namespace Test
{
    public class PropertySorter : ExpandableObjectConverter
    {

        #region Methods
        public override bool GetPropertiesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
        {
            //
            // Thise 這個overrid返回屬性的列表
            //
            PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(value, attributes);
            ArrayList orderedProperties = new ArrayList();
            foreach (PropertyDescriptor pd in pdc)
            {
                Attribute attribute = pd.Attributes[typeof(PropertyOrderAttribute)];
                if (attribute != null)
                {
                    //
                    // 如果找到的屬性,然後建立一個物件持有它
                    //
                    PropertyOrderAttribute poa = (PropertyOrderAttribute)attribute;
                    orderedProperties.Add(new PropertyOrderPair(pd.Name, poa.Order));
                }
                else
                {
                    //
                    // 沒有這個屬性就給它一個0
                    //
                    orderedProperties.Add(new PropertyOrderPair(pd.Name, 0));
                }
            }
            //
            // 執行實際的使用價值PropertyOrderPair類IComparable排序的實現
            //
            orderedProperties.Sort();
            //
            // 構建一個字串列表的命令名稱
            //
            ArrayList propertyNames = new ArrayList();
            foreach (PropertyOrderPair pop in orderedProperties)
            {
                propertyNames.Add(pop.Name);
            }
            //
            // 通過PropertyDescriptorCollection排序的有序列表
            //
            return pdc.Sort((string[])propertyNames.ToArray(typeof(string)));
        }
        #endregion
    }

    #region Helper Class - PropertyOrderAttribute
    [AttributeUsage(AttributeTargets.Property)]
    public class PropertyOrderAttribute : Attribute
    {
        //
        //簡單屬性允許指定一個屬性的順序
        //
        private int _order;
        public PropertyOrderAttribute(int order)
        {
            _order = order;
        }

        public int Order
        {
            get
            {
                return _order;
            }
        }
    }
    #endregion

    #region Helper Class - PropertyOrderPair
    public class PropertyOrderPair : IComparable
    {
        private int _order;
        private string _name;
        public string Name
        {
            get
            {
                return _name;
            }
        }

        public PropertyOrderPair(string name, int order)
        {
            _order = order;
            _name = name;
        }

        public int CompareTo(object obj)
        {
            //
            // 結對物件通過排序順序的值
            // 平等的價值觀得到相同的等級
            //
            int otherOrder = ((PropertyOrderPair)obj)._order;
            if (otherOrder == _order)
            {
                //
                // 如果未指定順序,按名稱排序
                //
                string otherName = ((PropertyOrderPair)obj)._name;
                return string.Compare(_name, otherName);
            }
            else if (otherOrder > _order)
            {
                return -1;
            }
            return 1;
        }
    }
    #endregion
}


//@! 瞎扯淡,我也不沒仔細看程式碼,不過我要的功能達到,可以看看效果圖

呼叫;
(在PropertyGrid屬性類裡面新增)

using System.ComponentModel;
using Utility.SW.Common;
using Utility.SW.NRegistry;

namespace Test
{
    [TypeConverter(typeof(PropertySorter))] //@! 用來排序
    public class TestConfig   
    {         
        private string _ip;
        private string _database;
        private string _uid;
        private string _pwd;

        #region 資料庫設定
        [CategoryAttribute("資料庫連線設定"), 
         DisplayNameAttribute("伺服器名稱"),       
         DescriptionAttribute("資料庫伺服器名(IP)"),
         PropertyOrderAttribute(1)  //@! 序號(排序)
        ]
        public string IP
        {
            get { return _ip; }
            set { _ip = value; }
        }

        [CategoryAttribute("資料庫連線設定"),
         DisplayNameAttribute("資料庫名稱"),       
         DescriptionAttribute("連線的資料庫名"),
         PropertyOrderAttribute(2)
        ]
        public string Database
        {
            get { return _database; }
            set { _database = value; }
        }

        [CategoryAttribute("資料庫連線設定"),
         DisplayNameAttribute("使用者名稱"),
         DescriptionAttribute("連線資料庫的使用者名稱"),
         PropertyOrderAttribute(3)
        ]
        public string UId
        {
            get { return _uid; }
            set { _uid = value; }
        }

        [CategoryAttribute("資料庫連線設定"),
         DisplayNameAttribute("密碼"),
         DescriptionAttribute("連線資料庫的密碼"),
         PropertyOrderAttribute(4)
        ]
        public string Pwd
        {
            get { return _pwd; }
            set { _pwd = value; }
        }
        #endregion
    }
}

效果;

這裡寫圖片描述

不排序的效果是
這裡寫圖片描述

4>PropertyGrid屬性排序

下面的連線是他們寫的點選直接轉到他這個只是在當前類上面新增

還有種是加(/t)

class Test
{
    [Category("Advanced")]
    public int Age { get; set; }

     //@! 
    [Category("\tGeneral")]       //<--
    public string Name { get; set; }
}

嘻嘻,這些東西都是網上有的資源,我也是借鑑..寫在一起了,以後就容易可以找到些,