溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

NET 框架 PropertyGrid 控件

發(fā)布時間:2020-06-13 17:09:37 來源:網(wǎng)絡 閱讀:659 作者:FGCFJ1987 欄目:編程語言

 

NET框架PropertyGrid控件是 Visual Studio .NET 屬性瀏覽器的核心。PropertyGrid控件顯示對象或類型的屬性,并主要通過使用反射來檢索項目的屬性

          首先創(chuàng)建 PropertyGrid 控件要使用 Visual Studio .NET 創(chuàng)建 PropertyGrid 控件,需要將該控件添加到工具箱中,因為默認情況下并不包含該控件。在 Tools(工具)菜單中,選擇 Customize Toolbox(自定義工具箱)。在對話框中選擇 Framework Components(框架組件)選項卡,然后選擇 PropertyGrid。

PropertyGrid 包含以下部分:

·        屬性

·        可展開屬性

·        屬性類別標題

·        屬性說明

·        屬性編輯器

·        屬性選項卡

·        命令窗格(顯示控件設計器提供的設計器操作)

PropertyGrid的外觀特征

·         PropertyGrid 的許多外觀特征都可以自定義。下面列出了其中的一部分:

·        通過 HelPBackColor、HelpForeColorHelpVisible屬性可以更改背景顏色、更改字體顏色或隱藏說明窗

·        通過 ToolbarVisible屬性可以隱藏工具欄,通過 BackColor屬性可以更改工具欄的顏色,通過 LargeButtons屬性可以顯示大工具欄按鈕。

·        使用 PropertySort屬性可以按字母順序?qū)傩赃M行排序和分類。

·        通過 BackColor屬性可以更改拆分器的顏色。

·        通過 LineColor屬性可以更改網(wǎng)格線和邊框。

·        本示例中的選項窗口不需要工具欄,因此可以將ToolbarVisible設置為 false。其余屬性均保留默認設置。

更改屬性的顯示方式

  要更改某些屬性的顯示方式,您可以對這些屬性應用不同的特性。特性是用于為類型、字段、方法和屬性等編程元素添加批注的聲明標記,在運行時可以使用反射對其進行檢索。下面列出了其中的一部分:

DescriptionAttribute - 設置顯示在屬性下方說明幫助窗格中的屬性文本。這是一種為活動屬性(即具有焦點的屬性)提供幫助文本的有效方法??梢詫⒋颂匦詰糜?MaxRepeatRate 屬性。

CategoryAttribute - 設置屬性在網(wǎng)格中所屬的類別。當您需要將屬性按類別名稱分組時,此特性非常有用。如果沒有為屬性指定類別,該屬性將被分配給雜項類別??梢詫⒋颂匦詰糜谒袑傩?。

BrowsableAttribute表示是否在網(wǎng)格中顯示屬性。此特性可用于在網(wǎng)格中隱藏屬性。默認情況下,公共屬性始終顯示在網(wǎng)格中??梢詫⒋颂匦詰糜?SettingsChanged 屬性。

ReadOnlyAttribute表示屬性是否為只讀。此特性可用于禁止在網(wǎng)格中編輯屬性。默認情況下,帶有 get 和 set 訪問函數(shù)的公共屬性在網(wǎng)格中是可以編輯的??梢詫⒋颂匦詰糜?AppVersion 屬性。

DefaultValueAttribute表示屬性的默認值。如果希望為屬性提供默認值,然后確定該屬性值是否與默認值相同,則可使用此特性??梢詫⒋颂匦詰糜谒袑傩浴?/span>

DefaultPropertyAttribute表示類的默認屬性。在網(wǎng)格中選擇某個類時,將首先突出顯示該類的默認屬性。

PropertyGrid有一個 property 為「SelectedObject」,用來指向所關連的物件。如下所示:

              Class newclass=newClass();     (例:propertyGrid控件)

propertyGrid1.SelectedObject = newclass;

 

PropertyGrid主要應用:

 

1.自定義一個布爾型的屬性下拉框:

                    private bool _bool;

                        [CategoryAttribute("布爾型"), DescriptionAttribute("布爾型")]

                     public bool 布爾型

                     {

                          get { return _bool; }

                          set { _bool = value;}

                     }

2自定義一個枚舉型的屬性下拉框:

             publicenum s

                        {

                              a = 1,

                         b = 2,

                         c = 4

 

                     }

                     private s test = s.a;

                     [CategoryAttribute("枚舉型"), DescriptionAttribute("這是一個枚舉的下拉框")]

                     public s 枚舉

                         {

                              get { return test; }

                                  set { test = value; }

                         }

3.自定義一個INT數(shù)組類型屬性對話框:

                  privateint[] arr;

                 [CategoryAttribute("數(shù)組型"), DescriptionAttribute("這是一個數(shù)組的對話框")]

                 public int[] 數(shù)組

                     {

                          get { return arr; }

                          set { arr = value; }

                       }

 

 

          4 屬性轉(zhuǎn)換器(TypeConverter)

        //自定義的StringConverter類型設置(包括自定義類型轉(zhuǎn)換器

        privatestring _fileName="1";

        [CategoryAttribute("自定義的復雜類型設置(包括自定義類型轉(zhuǎn)換器)"),TypeConverterAttribute(typeof(FileNameConverter)), ReadOnlyAttribute(false)]

        publicstring FileName

        {

 

            get{ return this._fileName;}

 

            set{ this._fileName = value;}

 

        }

//定義一個FileNameConverter繼承 StringConverter轉(zhuǎn)換器

        publicclass FileNameConverter: System.ComponentModel.StringConverter

        {

            ///<summary>

            ///根據(jù)返回值確定是否支持下拉框的形式

            ///</summary>

            ///<returns>

            /// true: 下來框的形式

            /// false: 普通文本編輯的形式

            ///</returns>

            publicoverride boolGetStandardValuesSupported(System.ComponentModel.ITypeDescriptorContextcontext)

            {

                return true;

            }

            ///下拉框中具體的內(nèi)容

            publicoverride System.ComponentModel.TypeConverter.StandardValuesCollectionGetStandardValues(System.ComponentModel.ITypeDescriptorContextcontext)

            {

                return new StandardValuesCollection(newstring[] { "1","2", "3"});

            }

            ///根據(jù)返回值確定是否是不可編輯的文本框

            /// true:  文本框不可以編輯

            /// flase: 文本框可以編輯

            publicoverride boolGetStandardValuesExclusive(System.ComponentModel.ITypeDescriptorContextcontext)

            {

                return true;

            }

        }

 

        5自定義一個帶圖片的屬性

        privateGrade x = newGrade(100);

     

        publicGrade X

        {

            get

            {

                return x;

            }

            set

            {

                x = value;

            }

        }

 

///指定用作此屬性所綁定到的對象的轉(zhuǎn)換器的類型

        [TypeConverterAttribute(typeof(ExpandableObjectConverter)),Category("自定義帶圖片屬性"), Description("自定義帶圖片屬性")]

//指定用來更改屬性的編輯器

        [Editor(typeof(GradeEditor),typeof(UITypeEditor))]

        publicstruct Grade

        {

            privateint grade;

            publicGrade(int grade)

            {

                this.grade = grade;

            }

            publicint Value

            {

                get

                {

                    return grade;

                }

            }

        }

        publicclass GradeEditor: UITypeEditor

        {

            //     指示指定的上下文是否支持在指定的上下文內(nèi)繪制對象值的表示形式。

            //

            //參數(shù):

            //   context:

            //     可用于獲取附加上下文信息的 System.ComponentModel.ITypeDescriptorContext。

            //

            //返回結(jié)果:

            //     如果實現(xiàn)System.Drawing.Design.UITypeEditor.PaintValue(System.Object,System.Drawing.Graphics,System.Drawing.Rectangle),則為

            //     true;否則為 false。

            publicoverride boolGetPaintValueSupported(ITypeDescriptorContextcontext)

            {

                return true;

            }

            //使用指定的System.Drawing.Design.PaintValueEventArgs 繪制某個對象的值的表示形式。

            publicoverride voidPaintValue(PaintValueEventArgs pe)

            {

                // 選擇根據(jù)valu的正確的位圖

                string bmpName = null;

          

                Grade g = (Grade)pe.Value;

                if (g.Value > 80)

                {    //可修改  bmpName   à圖片

                   // bmpName = @"C:\Documents andSettings\Administrator\桌面\ll\ll\2.ico";

                }

                else if (g.Value >60)

                {

                    //bmpName = @"C:\Documents and Settings\Administrator\桌面\ll\ll\3.ico";

                }

                else

                {

                   // bmpName = @"C:\Documents andSettings\Administrator\桌面\ll\ll\4.ico";

                }

 

                // 畫在表面上的位圖提供了// 多個圖片Bitmap b = new Bitmap(typeof(GradeEditor),bmpName)。

                Bitmap b = new Bitmap(global::propertyGrid.Properties.Resources.t1);

                //指示繪制內(nèi)容和繪制位置  pe.Bounds-->獲取圖片是矩形

               pe.Graphics.DrawImage(b, pe.Bounds);

                //釋放圖象的對象

                b.Dispose();

            }

        }

 

如圖1所示:NET 框架 PropertyGrid 控件      如圖2所示:NET 框架 PropertyGrid 控件

       6屬性框選項卡的擴展   -à屬性窗口的工具欄上有一個像閃電的按鈕,按下這個按鈕屬性窗口就會切換到屬性二

         //標識為要顯示的屬性選項卡   PropertyTabScope.Component:此選項卡被添加到當前的”屬性“窗口中去

         [PropertyTabAttribute(typeof(TypeCategoryTab),PropertyTabScope.Component)]

           classClass

{

private string _屬性二 ;

       [BrowsableAttribute(false)]

       public string屬性二

       {

              get{return  _屬性二; }

              set{ _屬性二=value;}

       }

       public classTypeCategoryTab :System.Windows.Forms.Design.PropertyTab

       {

            // 提供自定義一個 選項卡按扭  名字

            publicoverride stringTabName

            {

                get

                {

                    return"屬性(二)";

                }

            }

            // 提供選項卡按扭  的位圖

            publicoverride System.Drawing.Bitmap Bitmap

            {

                get

                {

                    Bitmapbmp = new Bitmap(global::propertyGrid.Properties.Resources.t1);

                    returnbmp;

                }

            }

            //獲取指定組件屬性的集合的對象

            //component:檢索屬性的組件

            //attributes:檢索的屬性(Property) 的屬性 (Attribute) 的System.Attribute 類型的數(shù)組

            publicoverride System.ComponentModel.PropertyDescriptorCollection GetProperties(object component, System.Attribute[]attributes)

            {///檢索  屬性框中屬性為隱藏的->[BrowsableAttribute(false)]的屬性集合

                PropertyDescriptorCollectionprops;

                Attribute[]att = new Attribute[]{ BrowsableAttribute.No};

                if(attributes == null)

                    props = TypeDescriptor.GetProperties(component);

                else

                    props = TypeDescriptor.GetProperties(component, att);

                return props;

            }

            ///  獲取指定組件的屬性。

            publicoverride System.ComponentModel.PropertyDescriptorCollection GetProperties(object component)

            {

                return this.GetProperties(component,null);

            }

 }

}

 


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI