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

(華版)

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

 

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

<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的源代碼:

Imports System
Imports System.ComponentModel

<TypeConverter(GetType(AcademicConverter)), _
[
ReadOnly](False), _
DescriptionAttribute(
"員工學歷。")> _
Public Class AcademicInfo
#Region 
"Properties"
    Private _GraduateSchool As String
    
Private _GraduateYear As String
    
Private _GraduateTitle As String

    
<DescriptionAttribute("學俯名字。")> _
    
Public Property 學俯() As String
        
Get
            
Return _GraduateSchool
        
End Get
        
Set(ByVal Value As String)
            _GraduateSchool 
= Value
        
End Set
    
End Property


    
<DescriptionAttribute("課程畢業年份。")> _
    
Public Property 畢業年份() As String
        
Get
            
Return _GraduateYear
        
End Get
        
Set(ByVal Value As String)
            _GraduateYear 
= Value
        
End Set
    
End Property


    
<DescriptionAttribute("受勳課程學位。")> _
    
Public Property 受勳學位() As String
        
Get
            
Return _GraduateTitle
        
End Get
        
Set(ByVal Value As String)
            _GraduateTitle 
= Value
        
End Set
    
End Property

#
End Region

End Class


Public Class AcademicConverter : Inherits ExpandableObjectConverter
#Region 
"Properties"
    Public Overloads Overrides Function CanConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal sourceType As System.Type) As Boolean
        
If (sourceType Is GetType(AcademicInfo)) Then
            
Return True
        
End If
        
Return MyBase.CanConvertFrom(context, sourceType)
    
End Function


    
Public Overloads Overrides Function ConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As ObjectAs Object
        
If TypeOf value Is AcademicInfo Then
            
Try
                
Dim s As String = CType(value, String)
                
Dim AcademicParts() As String
                AcademicParts 
= Split(s, ",")
                
If Not IsNothing(AcademicParts) Then
                    
Dim _AcademicInfo As AcademicInfo = New AcademicInfo
                    
If Not IsNothing(AcademicParts(0)) Then _AcademicInfo.受勳學位 = AcademicParts(0)
                    
If Not IsNothing(AcademicParts(1)) Then _AcademicInfo.畢業年份 = AcademicParts(1)
                    
If Not IsNothing(AcademicParts(2)) Then _AcademicInfo.學俯 = AcademicParts(2)
                
End If
            
Catch ex As Exception
                
Throw New ArgumentException("Can not convert '" + value + "' to type Person")
            
End Try
        
End If
        
Return MyBase.ConvertFrom(context, culture, value)
    
End Function


    
Public Overloads Overrides Function ConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As ObjectByVal destinationType As System.Type) As Object
        
If (destinationType Is GetType(System.StringAndAlso TypeOf value Is AcademicInfo) Then
            
Dim _AcademicInfo As AcademicInfo = CType(value, AcademicInfo)

            
'// 
            Return _AcademicInfo.受勳學位 & "," & _AcademicInfo.畢業年份 & "," & _AcademicInfo.學俯
        
End If

        
Return MyBase.ConvertTo(context, culture, value, destinationType)
    
End Function


    
Public Overloads Overrides Function CanConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal destinationType As System.Type) As Boolean

        
Return MyBase.CanConvertFrom(context, destinationType)
    
End Function

#
End Region

End Class

EmployeeProp Class的加添源代碼:
    <CategoryAttribute("員工個人學歷資料"), _
       Browsable(
True), _
       [
ReadOnly](False), _
       BindableAttribute(
False), _
       DefaultValueAttribute(
""), _
       DesignOnly(
False), _
       DescriptionAttribute(
"員工學院的學歷。")> _
    
Public Property 文憑學位() As AcademicInfo
        
Get
            
Return _Diploma
        
End Get
        
Set(ByVal Value As AcademicInfo)
            _Diploma 
= Value
        
End Set
    
End Property


    
<CategoryAttribute("員工個人學歷資料"), _
       Browsable(
True), _
       [
ReadOnly](False), _
       BindableAttribute(
False), _
       DefaultValueAttribute(
""), _
       DesignOnly(
False), _
       DescriptionAttribute(
"員工學士的學歷。")> _
    
Public Property 學士學位() As AcademicInfo
        
Get
            
Return _Bachelor
        
End Get
        
Set(ByVal Value As AcademicInfo)
            _Bachelor 
= Value
        
End Set
    
End Property


    
<CategoryAttribute("員工個人學歷資料"), _
       Browsable(
True), _
       [
ReadOnly](False), _
       BindableAttribute(
False), _
       DefaultValueAttribute(
""), _
       DesignOnly(
False), _
       DescriptionAttribute(
"員工硕士的學歷。")> _
    
Public Property 硕士學位() As AcademicInfo
        
Get
            
Return _Master
        
End Get
        
Set(ByVal Value As AcademicInfo)
            _Master 
= Value
        
End Set
    
End Property


    
<CategoryAttribute("員工個人學歷資料"), _
       Browsable(
True), _
       [
ReadOnly](False), _
       BindableAttribute(
False), _
       DefaultValueAttribute(
""), _
       DesignOnly(
False), _
       DescriptionAttribute(
"員工博士的學歷。")> _
    
Public Property 博士學位() As AcademicInfo
        
Get
            
Return _Doctor
        
End Get
        
Set(ByVal Value As AcademicInfo)
            _Doctor 
= Value
        
End Set
    
End Property


posted on 2005-03-04 15:44  克仔  阅读(1779)  评论(2编辑  收藏  举报