(華版)

過去如果要冩一個簡單的PropetyGrid控制項(圖一)可以説是难以登天(復雜的很)。。。但是現在.NET就可以用四個字來形容。。。非常容易!


  輕輕松松用PropertyGrid控制項。

你只需加入.NET内建的PropertyGrid控制項,然後在冩一個很簡單的EmployeeProp Class就可以了。但是此Class就有一點點特别。。。那就是在Class的聲明前加上此tag。

輕輕松松用PropertyGrid控制項。<DefaultPropertyAttribute("Title"), DescriptionAttribute("")>

然後于每一項Property都要加上以下的tag。
輕輕松松用PropertyGrid控制項。<CategoryAttribute(""), _
輕輕松松用PropertyGrid控制項。Browsable(
True|False), _
輕輕松松用PropertyGrid控制項。[
ReadOnly](True|False), _
輕輕松松用PropertyGrid控制項。BindableAttribute(
True|False), _
輕輕松松用PropertyGrid控制項。DefaultValueAttribute(
""), _
輕輕松松用PropertyGrid控制項。DesignOnly(
False), _
輕輕松松用PropertyGrid控制項。DescriptionAttribute(
"")>


當编冩好了EmployeeProp Class,下一步就是把PropertyGrid1.SelectedObject的值數設為剛聲明並實例EmployeeProp Class的對象變量就完成了。

點撃下載程序代碼

Form1的源代碼

 Form1
輕輕松松用PropertyGrid控制項。    Inherits System.Windows.Forms.Form
輕輕松松用PropertyGrid控制項。
輕輕松松用PropertyGrid控制項。    
' 宣告ep為EmployeeProp Class的變數
輕輕松松用PropertyGrid控制項。
    Private ep As EmployeeProp
輕輕松松用PropertyGrid控制項。
.Load
輕輕松松用PropertyGrid控制項。
輕輕松松用PropertyGrid控制項。        
'// 建立EmployeeProp Class的實軆
輕輕松松用PropertyGrid控制項。
        ep = New EmployeeProp
輕輕松松用PropertyGrid控制項。
輕輕松松用PropertyGrid控制項。        
'// 將SelectedObject的值數改為ep
輕輕松松用PropertyGrid控制項。
        PropertyGrid1.SelectedObject = ep
輕輕松松用PropertyGrid控制項。
輕輕松松用PropertyGrid控制項。    
End Sub
輕輕松松用PropertyGrid控制項。
 Button1.Click
輕輕松松用PropertyGrid控制項。
輕輕松松用PropertyGrid控制項。        
' 讀取所輸入員工資料
輕輕松松用PropertyGrid控制項。
        MessageBox.Show("員工" + ep.姓氏 + ep.名字 + "" + ep.職位 + "" + "聘請于" + ep.聘請日期)
輕輕松松用PropertyGrid控制項。
輕輕松松用PropertyGrid控制項。    
End Sub
輕輕松松用PropertyGrid控制項。
End Class

EmployeeProp Class的源代碼
輕輕松松用PropertyGrid控制項。Imports System.ComponentModel
輕輕松松用PropertyGrid控制項。
輕輕松松用PropertyGrid控制項。
<DefaultPropertyAttribute("Title"), _
輕輕松松用PropertyGrid控制項。DescriptionAttribute(
"員工資料")> _
 EmployeeProp
輕輕松松用PropertyGrid控制項。    
Private _FirstName As String
輕輕松松用PropertyGrid控制項。    
Private _LastName As String
輕輕松松用PropertyGrid控制項。    
Private _Age As String
輕輕松松用PropertyGrid控制項。    
Private _Gentle As String
輕輕松松用PropertyGrid控制項。    
Private _DateHire As Date
輕輕松松用PropertyGrid控制項。    
Private _Position As String
輕輕松松用PropertyGrid控制項。    
Private _Department As String
輕輕松松用PropertyGrid控制項。    
Private _Salary As String
輕輕松松用PropertyGrid控制項。
輕輕松松用PropertyGrid控制項。    
<CategoryAttribute("員工個人資料"), _
輕輕松松用PropertyGrid控制項。       Browsable(
True), _
輕輕松松用PropertyGrid控制項。       [
ReadOnly](False), _
輕輕松松用PropertyGrid控制項。       BindableAttribute(
False), _
輕輕松松用PropertyGrid控制項。       DefaultValueAttribute(
""), _
輕輕松松用PropertyGrid控制項。       DesignOnly(
False), _
輕輕松松用PropertyGrid控制項。       DescriptionAttribute(
"員工的名字。")> _

輕輕松松用PropertyGrid控制項。        
Get
輕輕松松用PropertyGrid控制項。            
Return _FirstName
輕輕松松用PropertyGrid控制項。        
End Get
輕輕松松用PropertyGrid控制項。        
Set(ByVal Value As String)
輕輕松松用PropertyGrid控制項。            _FirstName 
= 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(
"員工的姓氏。")> _

輕輕松松用PropertyGrid控制項。        
Get
輕輕松松用PropertyGrid控制項。            
Return _LastName
輕輕松松用PropertyGrid控制項。        
End Get
輕輕松松用PropertyGrid控制項。        
Set(ByVal Value As String)
輕輕松松用PropertyGrid控制項。            _LastName 
= 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(
"員工的年龄。")> _

輕輕松松用PropertyGrid控制項。        
Get
輕輕松松用PropertyGrid控制項。            
Return _Age
輕輕松松用PropertyGrid控制項。        
End Get
輕輕松松用PropertyGrid控制項。        
Set(ByVal Value As String)
輕輕松松用PropertyGrid控制項。            _Age 
= 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(
"員工的性别。")> _

輕輕松松用PropertyGrid控制項。        
Get
輕輕松松用PropertyGrid控制項。            
Return _Gentle
輕輕松松用PropertyGrid控制項。        
End Get
輕輕松松用PropertyGrid控制項。        
Set(ByVal Value As String)
輕輕松松用PropertyGrid控制項。            _Gentle 
= 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(
"員工聘請日期。")> _

輕輕松松用PropertyGrid控制項。        
Get
輕輕松松用PropertyGrid控制項。            
Return _DateHire
輕輕松松用PropertyGrid控制項。        
End Get
輕輕松松用PropertyGrid控制項。        
Set(ByVal Value As Date)
輕輕松松用PropertyGrid控制項。            _DateHire 
= 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(
"員工的执行職位。")> _

輕輕松松用PropertyGrid控制項。        
Get
輕輕松松用PropertyGrid控制項。            
Return _Position
輕輕松松用PropertyGrid控制項。        
End Get
輕輕松松用PropertyGrid控制項。        
Set(ByVal Value As String)
輕輕松松用PropertyGrid控制項。            _Position 
= 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(
"員工所服務的部門。")> _

輕輕松松用PropertyGrid控制項。        
Get
輕輕松松用PropertyGrid控制項。            
Return _Department
輕輕松松用PropertyGrid控制項。        
End Get
輕輕松松用PropertyGrid控制項。        
Set(ByVal Value As String)
輕輕松松用PropertyGrid控制項。            _Department 
= 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(
"員工所领取的薪金。")> _

輕輕松松用PropertyGrid控制項。        
Get
輕輕松松用PropertyGrid控制項。            
Return _Salary
輕輕松松用PropertyGrid控制項。        
End Get
輕輕松松用PropertyGrid控制項。        
Set(ByVal Value As String)
輕輕松松用PropertyGrid控制項。            _Salary 
= Value
輕輕松松用PropertyGrid控制項。        
End Set
輕輕松松用PropertyGrid控制項。    
End Property
輕輕松松用PropertyGrid控制項。
輕輕松松用PropertyGrid控制項。
End Class
輕輕松松用PropertyGrid控制項。

然後于每一項Property都要加上以下的tag。 :

 

相关文章: