Program,Life,Society.....

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

PropertyGrid 控件简介
如果您使用过 Microsoft? Visual Basic? 或 Microsoft Visual Studio .NET,那么您一定使用过属性浏览器来浏览、
查看和编辑一个或多个对象的属性。.NET 框架 PropertyGrid 控件是 Visual Studio .NET 属性浏览器的核心。
PropertyGrid 控件显示对象或类型的属性,并主要通过使用反射来检索项目的属性。

    这是出来的效果.

这是一个显示UltraTreeView控件的部分属性的自定义属性框,我这里创建了一个TreeNodeAppearance的类,用于创建我
需要的属性.(这里的属性都是控件本身原有的属性,当然你可以创建自己的新属性去适应你的程序)

Public Class TreeAppearance
  
    
Private _Expand As Boolean
    
    
Public TV As
 Infragistics.Win.UltraWinTree.UltraTree

    
Property ForeColor() As Color
        
Get

            
Return TV.Appearance.ForeColor
        
End Get

        
Set(ByVal Value As Color)
            TV.Appearance.ForeColor 
=
 Value
        
End Set

    
End Property

    
Property BackgroundImageStyle() As Infragistics.Win.ImageBackgroundStyle
        
Get

            
Return TV.Appearance.BackGradientStyle
        
End Get

        
Set(ByVal Value As Infragistics.Win.ImageBackgroundStyle)
            TV.Appearance.BackGradientStyle 
=
 Value
        
End Set

    
End Property

 
    
Property Expand() As Boolean
        
Get
            
Return _Expand
        
End Get

        
Set(ByVal Value As Boolean)
            _Expand 
=
 Value
            
If _Expand Then

                TV.ExpandAll()
            
Else
                TV.ExpandAll()
            
End If
        
End Set
    
End Property

     
'
     '.
End Class


为了显示属性,你必须创建一个新的窗体,我的这个新窗体(frmTreeApperance)中只含有一个PropertyGrid控件来显示属性.

下面的操作是基于SQL Server 数据库的,为了达到记忆,读取,设置属性,创建一个属性表(这个表的创建我觉得有两种方法,
一是将各个属性名作为字段名一一列举,而是创建统一的PropName 和PropValue字段用于存储,具体的采用何方法可根据自己
的需要,如果是属性类型多样花,建议使用前者))
1.弹出窗口前的初始化工作,让属性窗口中含有已有的属性.这这里我写了一个 SetAppearance过程来完整

 Private Sub LoadTreeViewApperance()
        
Dim BackColor As Integer

        
Dim ForeColor As Integer
        
Dim BackgroundImageStyle As Infragistics.Win.ImageBackgroundStyle
        
Dim Indent As Integer

        
Dim NodeConnectorStyle As Infragistics.Win.UltraWinTree.NodeConnectorStyle
        
Dim Expand As Boolean

        
Dim ShowLines As Boolean
        
Dim ShowRootLines As Boolean
        
Dim BorderStyle As Infragistics.Win.UIElementBorderStyle

        
Dim sPropName, sPropValue As String


        
Dim sql As String
        
'这里我用的是第二中方法来存储属性,sql语句像下面
        sql = "Select [PropName],[Propvalue] From [TreeViewSetting] Where  [AppID]=" & AppID
        
Dim t As
 DataTable
        t 
= ExecuteDataset(ProviderType.SQL, CommandData.ConnectiongString, CommandType.Text, sql, "t").Tables(0
)
        
'你可以用SQLhelper来完成,我这里用的是公司内部的函数,我没有改变

        If t.Rows.Count = 0 Then Exit Sub
        
Dim r As DataRow
        
For Each r In
 t.Rows
            
If IsDBNull(r.Item(0)) Or IsDBNull(r.Item(1)) Then

                sPropName 
= "No PropName"
            Else
                sPropName 
= r.Item(0)
                sPropValue 
= r.Item(1
)
            
End If


            
Select Case sPropName
                
Case "BackColor"

                    BackColor = CInt(sPropValue)
                    
'颜色的属性用整数存储,还有其他不同的类型为了都可以存储为String你必须都要有相应的自己解析函数

                    If BackColor <> -1 Then TA.BackColor = Color.FromArgb(BackColor)
                
Case "ForeColor"

                    ForeColor = CInt(sPropValue)
                    
If ForeColor <> -1 Then TA.ForeColor =
 Color.FromArgb(ForeColor)
                
Case "BackgroundImage"

                    LoadImage(sPropName, sPropValue)'Image文件我做了特殊的处理用这个里这个不是主题
             '
             '
       
        
Next

        
'TA是全程的私有变量,是我们定义的那个存放属性的类    Private TA As New TreeAppearance
        DTree = TA.TV
    
End Sub

 2.弹出窗口的工作,Load属性类到PropertyGrid 控件上
     Private Sub SetTreeViewAppearance()
        
Dim frmTreeApperance As New
 TreeViewAppearance
        frmTreeApperance.pgdTree.SelectedObject 
= TA 'pgdTree是PropertyGrid 控件

        frmTreeApperance.ShowDialog()
        SaveTreeViewAppearance()
' SaveTreeViewAppearance 存储设置的属性到数据库.

         End Sub

3.关闭窗口窗口后的工作,存储设置的属性到数据库.
  Private Sub SaveTreeViewAppearance()
        
Dim ForeColor As Integer

        
Dim BackColor As Integer
        
Dim BackgroundImageStyle As String
        
Dim ProperTab As New Hashtable
        
Dim PropertyStr As String

               
        
With TA
            ForeColor 
=
 .ForeColor.ToArgb
            BackColor 
=
 .BackColor.ToArgb
            BackgroundImageStyle 
=
 .BackgroundImageStyle.ToString
            Indent 
=
 .Indent
            NodeConnectorStyle 
=
 .NodeConnectorStyle.ToString
            
If .Expand Then Expand = 1

            
If .ShowLines Then ShowLines = 1
            
If .ShowRootLines Then ShowRootLines = 1
            BorderStyle 
= .BorderStyle.ToString
        
End With



Dim ProperTab as new HashTable
             ProperTab.Add(
"ForeColor"
, ForeColor)
            ProperTab.Add(
"BackColor"
, BackColor)
            ProperTab.Add(
"BackgroundImageStyle"
, BackgroundImageStyle)
            ProperTab.Add(
"Indent"
, Indent)
      .

            
Try

            
For Each PropertyStr In ProperTab.Keys
                
Dim CommStr As String = "Select count(1) from [TreeviewSetting] where [AppID]=" & AppID & " and [PropName]='" & PropertyStr & "'"

                Dim InsertStr As String = "Insert into [TreeviewSetting] ([AppID],[PropName],[PropValue]) values (" & AppID & ",'" & PropertyStr & "','" & ProperTab.Item(PropertyStr).ToString.Replace("'""''"& "')"
                Dim UpdateStr As String = "Update [TreeviewSetting] set [PropValue]='" & ProperTab.Item(PropertyStr).ToString.Replace("'""''"& "' where [AppID]=" & AppID & " and [PropName]='" & PropertyStr & "'"
                Dim I As Integer

                I 
= ExecuteScalar(ProviderType.SQL, CommandData.ConnectiongString, CommandType.Text, CommStr)
                
If I = 0 Then

                    ExecuteNonQuery(ProviderType.SQL, CommandData.ConnectiongString, CommandType.Text, InsertStr)
                
Else
                    ExecuteNonQuery(ProviderType.SQL, CommandData.ConnectiongString, CommandType.Text, UpdateStr)
                
End If
            
Next
        
Catch ex As Exception
            MessageBox.Show(ex.Message, CommandData.ProjectName, MessageBoxButtons.OK, MessageBoxIcon.
Stop
)
        
Finally

            ProperTab 
= Nothing
        
End Try

    
End Sub

这样就完成的属性的读取,设置,存储的工作.
更多的关于PropertyGrid 控件,可以看看微软的东西:
充分利用 .NET 框架的 PropertyGrid 控件






posted on 2004-09-27 11:02  vuejs3  阅读(2744)  评论(1编辑  收藏  举报