輕輕松松用PropertyGrid控制項。

(華版)

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


 

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

<DefaultPropertyAttribute("Title"), DescriptionAttribute("")>

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


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

點撃下載程序代碼

Form1的源代碼

Public Class Form1
    
Inherits System.Windows.Forms.Form

    
' 宣告ep為EmployeeProp Class的變數
    Private ep As EmployeeProp

    
Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load

        
'// 建立EmployeeProp Class的實軆
        ep = New EmployeeProp

        
'// 將SelectedObject的值數改為ep
        PropertyGrid1.SelectedObject = ep

    
End Sub


    
Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click

        
' 讀取所輸入員工資料
        MessageBox.Show("員工" + ep.姓氏 + ep.名字 + "" + ep.職位 + "" + "聘請于" + ep.聘請日期)

    
End Sub

End Class

EmployeeProp Class的源代碼
Imports System.ComponentModel

<DefaultPropertyAttribute("Title"), _
DescriptionAttribute(
"員工資料")> _
Public Class EmployeeProp
    
Private _FirstName As String
    
Private _LastName As String
    
Private _Age As String
    
Private _Gentle As String
    
Private _DateHire As Date
    
Private _Position As String
    
Private _Department As String
    
Private _Salary As String

    
<CategoryAttribute("員工個人資料"), _
       Browsable(
True), _
       [
ReadOnly](False), _
       BindableAttribute(
False), _
       DefaultValueAttribute(
""), _
       DesignOnly(
False), _
       DescriptionAttribute(
"員工的名字。")> _
    
Public Property 名字() As String
        
Get
            
Return _FirstName
        
End Get
        
Set(ByVal Value As String)
            _FirstName 
= Value
        
End Set
    
End Property


    
<CategoryAttribute("員工個人資料"), _
       Browsable(
True), _
       [
ReadOnly](False), _
       BindableAttribute(
False), _
       DefaultValueAttribute(
""), _
       DesignOnly(
False), _
       DescriptionAttribute(
"員工的姓氏。")> _
    
Public Property 姓氏() As String
        
Get
            
Return _LastName
        
End Get
        
Set(ByVal Value As String)
            _LastName 
= Value
        
End Set
    
End Property


    
<CategoryAttribute("員工個人資料"), _
       Browsable(
True), _
       [
ReadOnly](False), _
       BindableAttribute(
False), _
       DefaultValueAttribute(
""), _
       DesignOnly(
False), _
       DescriptionAttribute(
"員工的年龄。")> _
    
Public Property 年龄() As String
        
Get
            
Return _Age
        
End Get
        
Set(ByVal Value As String)
            _Age 
= Value
        
End Set
    
End Property


    
<CategoryAttribute("員工個人資料"), _
       Browsable(
True), _
       [
ReadOnly](False), _
       BindableAttribute(
False), _
       DefaultValueAttribute(
""), _
       DesignOnly(
False), _
       DescriptionAttribute(
"員工的性别。")> _
    
Public Property 性别() As String
        
Get
            
Return _Gentle
        
End Get
        
Set(ByVal Value As String)
            _Gentle 
= Value
        
End Set
    
End Property


    
<CategoryAttribute("人事部資料"), _
       Browsable(
True), _
       [
ReadOnly](False), _
       BindableAttribute(
False), _
       DefaultValueAttribute(
""), _
       DesignOnly(
False), _
       DescriptionAttribute(
"員工聘請日期。")> _
    
Public Property 聘請日期() As Date
        
Get
            
Return _DateHire
        
End Get
        
Set(ByVal Value As Date)
            _DateHire 
= Value
        
End Set
    
End Property


    
<CategoryAttribute("人事部資料"), _
       Browsable(
True), _
       [
ReadOnly](False), _
       BindableAttribute(
False), _
       DefaultValueAttribute(
""), _
       DesignOnly(
False), _
       DescriptionAttribute(
"員工的执行職位。")> _
    
Public Property 職位() As String
        
Get
            
Return _Position
        
End Get
        
Set(ByVal Value As String)
            _Position 
= Value
        
End Set
    
End Property


    
<CategoryAttribute("人事部資料"), _
       Browsable(
True), _
       [
ReadOnly](False), _
       BindableAttribute(
False), _
       DefaultValueAttribute(
""), _
       DesignOnly(
False), _
       DescriptionAttribute(
"員工所服務的部門。")> _
    
Public Property 部門() As String
        
Get
            
Return _Department
        
End Get
        
Set(ByVal Value As String)
            _Department 
= Value
        
End Set
    
End Property


    
<CategoryAttribute("人事部資料"), _
       Browsable(
True), _
       [
ReadOnly](False), _
       BindableAttribute(
False), _
       DefaultValueAttribute(
""), _
       DesignOnly(
False), _
       DescriptionAttribute(
"員工所领取的薪金。")> _
    
Public Property 薪金() As String
        
Get
            
Return _Salary
        
End Get
        
Set(ByVal Value As String)
            _Salary 
= Value
        
End Set
    
End Property


End Class

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

 

posted on 2005-03-03 23:42  克仔  阅读(1597)  评论(3)    收藏  举报