(華版)

上一回簡介了PropertyGrid的基本入門法;現在再進一步去增强此PropertyGrid的界面与更好的輸入方法。就是怎样可以達到在一個項目里再分出多個小項目!比如説FontLocationSize這種效果。

  輕輕松松用PropertyGrid控制項 - 加强版。

方法很簡單。。。之需要廣建上一回的EmployeeProp Class和另冩多一個AcademicInfo Class就可以了。先看新冩的AcademicInfo Class;在此Class里,我們要加上一下的一個tag。

輕輕松松用PropertyGrid控制項 - 加强版。<TypeConverter(GetType(AcademicConverter)), [ReadOnly](False), DescriptionAttribute("員工學歷。")> _


然後就冩此Class的properties,如常只寫Get和Set功能;但是在每一個新的property都要加上<DescriptionAttribute("")>tag。此tag是用來收藏此property的值數説明。

而在整個AcademicInfo Class里最為關键的就是<TypeConverter(GetType(AcademicConverter))。因為AcademicConverter Class里有两個很重要的函數:
  1. ConvertTo
  2. ConvertFrom

ConvertTo函數是用來合并AcademicInfo Class里的每一個Property的值數成為一個字串而用comma ","來分别每一個值數;但是ConvertFrom就是剛剛倒翻ConvertTo的功能。

最後,在EmployeeProp Class里聲明和實例AcademicInfo Class的對象就可以了。

點撃下載程序源代碼

AcademicInfo Class的源代碼:

輕輕松松用PropertyGrid控制項 - 加强版。Imports System
輕輕松松用PropertyGrid控制項 - 加强版。
Imports System.ComponentModel
輕輕松松用PropertyGrid控制項 - 加强版。
輕輕松松用PropertyGrid控制項 - 加强版。
<TypeConverter(GetType(AcademicConverter)), _
輕輕松松用PropertyGrid控制項 - 加强版。[
ReadOnly](False), _
輕輕松松用PropertyGrid控制項 - 加强版。DescriptionAttribute(
"員工學歷。")> _
 AcademicInfo
輕輕松松用PropertyGrid控制項 - 加强版。#Region 
"Properties"
輕輕松松用PropertyGrid控制項 - 加强版。
    Private _GraduateSchool As String
輕輕松松用PropertyGrid控制項 - 加强版。    
Private _GraduateYear As String
輕輕松松用PropertyGrid控制項 - 加强版。    
Private _GraduateTitle As String
輕輕松松用PropertyGrid控制項 - 加强版。
輕輕松松用PropertyGrid控制項 - 加强版。    
<DescriptionAttribute("學俯名字。")> _

輕輕松松用PropertyGrid控制項 - 加强版。        
Get
輕輕松松用PropertyGrid控制項 - 加强版。            
Return _GraduateSchool
輕輕松松用PropertyGrid控制項 - 加强版。        
End Get
輕輕松松用PropertyGrid控制項 - 加强版。        
Set(ByVal Value As String)
輕輕松松用PropertyGrid控制項 - 加强版。            _GraduateSchool 
= Value
輕輕松松用PropertyGrid控制項 - 加强版。        
End Set
輕輕松松用PropertyGrid控制項 - 加强版。    
End Property
輕輕松松用PropertyGrid控制項 - 加强版。
輕輕松松用PropertyGrid控制項 - 加强版。    
<DescriptionAttribute("課程畢業年份。")> _

輕輕松松用PropertyGrid控制項 - 加强版。        
Get
輕輕松松用PropertyGrid控制項 - 加强版。            
Return _GraduateYear
輕輕松松用PropertyGrid控制項 - 加强版。        
End Get
輕輕松松用PropertyGrid控制項 - 加强版。        
Set(ByVal Value As String)
輕輕松松用PropertyGrid控制項 - 加强版。            _GraduateYear 
= Value
輕輕松松用PropertyGrid控制項 - 加强版。        
End Set
輕輕松松用PropertyGrid控制項 - 加强版。    
End Property
輕輕松松用PropertyGrid控制項 - 加强版。
輕輕松松用PropertyGrid控制項 - 加强版。    
<DescriptionAttribute("受勳課程學位。")> _

輕輕松松用PropertyGrid控制項 - 加强版。        
Get
輕輕松松用PropertyGrid控制項 - 加强版。            
Return _GraduateTitle
輕輕松松用PropertyGrid控制項 - 加强版。        
End Get
輕輕松松用PropertyGrid控制項 - 加强版。        
Set(ByVal Value As String)
輕輕松松用PropertyGrid控制項 - 加强版。            _GraduateTitle 
= Value
輕輕松松用PropertyGrid控制項 - 加强版。        
End Set
輕輕松松用PropertyGrid控制項 - 加强版。    
End Property
輕輕松松用PropertyGrid控制項 - 加强版。#
End Region
輕輕松松用PropertyGrid控制項 - 加强版。
輕輕松松用PropertyGrid控制項 - 加强版。
End Class
輕輕松松用PropertyGrid控制項 - 加强版。
 ExpandableObjectConverter
輕輕松松用PropertyGrid控制項 - 加强版。#Region 
"Properties"

輕輕松松用PropertyGrid控制項 - 加强版。        
If (sourceType Is GetType(AcademicInfo)) Then
輕輕松松用PropertyGrid控制項 - 加强版。            
Return True
輕輕松松用PropertyGrid控制項 - 加强版。        
End If
輕輕松松用PropertyGrid控制項 - 加强版。        
Return MyBase.CanConvertFrom(context, sourceType)
輕輕松松用PropertyGrid控制項 - 加强版。    
End Function
輕輕松松用PropertyGrid控制項 - 加强版。

輕輕松松用PropertyGrid控制項 - 加强版。        
If TypeOf value Is AcademicInfo Then
輕輕松松用PropertyGrid控制項 - 加强版。            
Try
輕輕松松用PropertyGrid控制項 - 加强版。                
Dim s As String = CType(value, String)
輕輕松松用PropertyGrid控制項 - 加强版。                
Dim AcademicParts() As String
輕輕松松用PropertyGrid控制項 - 加强版。                AcademicParts 
= Split(s, ",")
輕輕松松用PropertyGrid控制項 - 加强版。                
If Not IsNothing(AcademicParts) Then
輕輕松松用PropertyGrid控制項 - 加强版。                    
Dim _AcademicInfo As AcademicInfo = New AcademicInfo
輕輕松松用PropertyGrid控制項 - 加强版。                    
If Not IsNothing(AcademicParts(0)) Then _AcademicInfo.受勳學位 = AcademicParts(0)
輕輕松松用PropertyGrid控制項 - 加强版。                    
If Not IsNothing(AcademicParts(1)) Then _AcademicInfo.畢業年份 = AcademicParts(1)
輕輕松松用PropertyGrid控制項 - 加强版。                    
If Not IsNothing(AcademicParts(2)) Then _AcademicInfo.學俯 = AcademicParts(2)
輕輕松松用PropertyGrid控制項 - 加强版。                
End If
輕輕松松用PropertyGrid控制項 - 加强版。            
Catch ex As Exception
輕輕松松用PropertyGrid控制項 - 加强版。                
Throw New ArgumentException("Can not convert '" + value + "' to type Person")
輕輕松松用PropertyGrid控制項 - 加强版。            
End Try
輕輕松松用PropertyGrid控制項 - 加强版。        
End If
輕輕松松用PropertyGrid控制項 - 加强版。        
Return MyBase.ConvertFrom(context, culture, value)
輕輕松松用PropertyGrid控制項 - 加强版。    
End Function
輕輕松松用PropertyGrid控制項 - 加强版。

輕輕松松用PropertyGrid控制項 - 加强版。        
If (destinationType Is GetType(System.StringAndAlso TypeOf value Is AcademicInfo) Then
輕輕松松用PropertyGrid控制項 - 加强版。            
Dim _AcademicInfo As AcademicInfo = CType(value, AcademicInfo)
輕輕松松用PropertyGrid控制項 - 加强版。
輕輕松松用PropertyGrid控制項 - 加强版。            
'// 
輕輕松松用PropertyGrid控制項 - 加强版。
            Return _AcademicInfo.受勳學位 & "," & _AcademicInfo.畢業年份 & "," & _AcademicInfo.學俯
輕輕松松用PropertyGrid控制項 - 加强版。        
End If
輕輕松松用PropertyGrid控制項 - 加强版。
輕輕松松用PropertyGrid控制項 - 加强版。        
Return MyBase.ConvertTo(context, culture, value, destinationType)
輕輕松松用PropertyGrid控制項 - 加强版。    
End Function
輕輕松松用PropertyGrid控制項 - 加强版。

輕輕松松用PropertyGrid控制項 - 加强版。
輕輕松松用PropertyGrid控制項 - 加强版。        
Return MyBase.CanConvertFrom(context, destinationType)
輕輕松松用PropertyGrid控制項 - 加强版。    
End Function
輕輕松松用PropertyGrid控制項 - 加强版。#
End Region
輕輕松松用PropertyGrid控制項 - 加强版。
輕輕松松用PropertyGrid控制項 - 加强版。
End Class

EmployeeProp Class的加添源代碼:
輕輕松松用PropertyGrid控制項 - 加强版。    <CategoryAttribute("員工個人學歷資料"), _
輕輕松松用PropertyGrid控制項 - 加强版。       Browsable(
True), _
輕輕松松用PropertyGrid控制項 - 加强版。       [
ReadOnly](False), _
輕輕松松用PropertyGrid控制項 - 加强版。       BindableAttribute(
False), _
輕輕松松用PropertyGrid控制項 - 加强版。       DefaultValueAttribute(
""), _
輕輕松松用PropertyGrid控制項 - 加强版。       DesignOnly(
False), _
輕輕松松用PropertyGrid控制項 - 加强版。       DescriptionAttribute(
"員工學院的學歷。")> _
 AcademicInfo
輕輕松松用PropertyGrid控制項 - 加强版。        
Get
輕輕松松用PropertyGrid控制項 - 加强版。            
Return _Diploma
輕輕松松用PropertyGrid控制項 - 加强版。        
End Get
輕輕松松用PropertyGrid控制項 - 加强版。        
Set(ByVal Value As AcademicInfo)
輕輕松松用PropertyGrid控制項 - 加强版。            _Diploma 
= Value
輕輕松松用PropertyGrid控制項 - 加强版。        
End Set
輕輕松松用PropertyGrid控制項 - 加强版。    
End Property
輕輕松松用PropertyGrid控制項 - 加强版。
輕輕松松用PropertyGrid控制項 - 加强版。    
<CategoryAttribute("員工個人學歷資料"), _
輕輕松松用PropertyGrid控制項 - 加强版。       Browsable(
True), _
輕輕松松用PropertyGrid控制項 - 加强版。       [
ReadOnly](False), _
輕輕松松用PropertyGrid控制項 - 加强版。       BindableAttribute(
False), _
輕輕松松用PropertyGrid控制項 - 加强版。       DefaultValueAttribute(
""), _
輕輕松松用PropertyGrid控制項 - 加强版。       DesignOnly(
False), _
輕輕松松用PropertyGrid控制項 - 加强版。       DescriptionAttribute(
"員工學士的學歷。")> _
 AcademicInfo
輕輕松松用PropertyGrid控制項 - 加强版。        
Get
輕輕松松用PropertyGrid控制項 - 加强版。            
Return _Bachelor
輕輕松松用PropertyGrid控制項 - 加强版。        
End Get
輕輕松松用PropertyGrid控制項 - 加强版。        
Set(ByVal Value As AcademicInfo)
輕輕松松用PropertyGrid控制項 - 加强版。            _Bachelor 
= Value
輕輕松松用PropertyGrid控制項 - 加强版。        
End Set
輕輕松松用PropertyGrid控制項 - 加强版。    
End Property
輕輕松松用PropertyGrid控制項 - 加强版。
輕輕松松用PropertyGrid控制項 - 加强版。    
<CategoryAttribute("員工個人學歷資料"), _
輕輕松松用PropertyGrid控制項 - 加强版。       Browsable(
True), _
輕輕松松用PropertyGrid控制項 - 加强版。       [
ReadOnly](False), _
輕輕松松用PropertyGrid控制項 - 加强版。       BindableAttribute(
False), _
輕輕松松用PropertyGrid控制項 - 加强版。       DefaultValueAttribute(
""), _
輕輕松松用PropertyGrid控制項 - 加强版。       DesignOnly(
False), _
輕輕松松用PropertyGrid控制項 - 加强版。       DescriptionAttribute(
"員工硕士的學歷。")> _
 AcademicInfo
輕輕松松用PropertyGrid控制項 - 加强版。        
Get
輕輕松松用PropertyGrid控制項 - 加强版。            
Return _Master
輕輕松松用PropertyGrid控制項 - 加强版。        
End Get
輕輕松松用PropertyGrid控制項 - 加强版。        
Set(ByVal Value As AcademicInfo)
輕輕松松用PropertyGrid控制項 - 加强版。            _Master 
= Value
輕輕松松用PropertyGrid控制項 - 加强版。        
End Set
輕輕松松用PropertyGrid控制項 - 加强版。    
End Property
輕輕松松用PropertyGrid控制項 - 加强版。
輕輕松松用PropertyGrid控制項 - 加强版。    
<CategoryAttribute("員工個人學歷資料"), _
輕輕松松用PropertyGrid控制項 - 加强版。       Browsable(
True), _
輕輕松松用PropertyGrid控制項 - 加强版。       [
ReadOnly](False), _
輕輕松松用PropertyGrid控制項 - 加强版。       BindableAttribute(
False), _
輕輕松松用PropertyGrid控制項 - 加强版。       DefaultValueAttribute(
""), _
輕輕松松用PropertyGrid控制項 - 加强版。       DesignOnly(
False), _
輕輕松松用PropertyGrid控制項 - 加强版。       DescriptionAttribute(
"員工博士的學歷。")> _
 AcademicInfo
輕輕松松用PropertyGrid控制項 - 加强版。        
Get
輕輕松松用PropertyGrid控制項 - 加强版。            
Return _Doctor
輕輕松松用PropertyGrid控制項 - 加强版。        
End Get
輕輕松松用PropertyGrid控制項 - 加强版。        
Set(ByVal Value As AcademicInfo)
輕輕松松用PropertyGrid控制項 - 加强版。            _Doctor 
= Value
輕輕松松用PropertyGrid控制項 - 加强版。        
End Set
輕輕松松用PropertyGrid控制項 - 加强版。    
End Property
輕輕松松用PropertyGrid控制項 - 加强版。

相关文章: