读取,设置,存储--利用PropertyGrid 控件对属性的操作(基于SQL Server).

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

PropGRD.jpg    这是出来的效果.

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

ExpandedBlockStart.gifContractedBlock.gifPublic Class TreeAppearance Class TreeAppearance
InBlock.gif  
InBlock.gif    
Private _Expand As Boolean
    
InBlock.gif    
Public TV As
 Infragistics.Win.UltraWinTree.UltraTree
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Property ForeColor()
Property ForeColor() As Color
InBlock.gif        
Get

InBlock.gif            
Return TV.Appearance.ForeColor
InBlock.gif        
End Get

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

ExpandedSubBlockEnd.gif    
End Property

ExpandedSubBlockStart.gifContractedSubBlock.gif    
Property BackgroundImageStyle()Property BackgroundImageStyle() As Infragistics.Win.ImageBackgroundStyle
InBlock.gif        
Get

InBlock.gif            
Return TV.Appearance.BackGradientStyle
InBlock.gif        
End Get

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

ExpandedSubBlockEnd.gif    
End Property

InBlock.gif 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Property Expand()Property Expand() As Boolean
InBlock.gif        
Get
InBlock.gif            
Return _Expand
InBlock.gif        
End Get

InBlock.gif        
Set(ByVal Value As Boolean)
InBlock.gif            _Expand 
=
 Value
InBlock.gif            
If _Expand Then

InBlock.gif                TV.ExpandAll()
InBlock.gif            
Else
InBlock.gif                TV.ExpandAll()
InBlock.gif            
End If
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif     
'dot.gifdot.gifdot.gifdot.gif
InBlock.gif
     'dot.gifdot.gif.
ExpandedBlockEnd.gif
End Class

None.gif


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

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

ExpandedBlockStart.gifContractedBlock.gif Private Sub LoadTreeViewApperance() Sub LoadTreeViewApperance()
InBlock.gif        
Dim BackColor As Integer

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

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

InBlock.gif        
Dim ShowLines As Boolean
InBlock.gif        
Dim ShowRootLines As Boolean
InBlock.gif        
Dim BorderStyle As Infragistics.Win.UIElementBorderStyle
InBlock.gif
InBlock.gif        
Dim sPropName, sPropValue As String

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

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

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

InBlock.gif
InBlock.gif            
Select Case sPropName
InBlock.gif                
Case "BackColor"

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

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

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

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

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

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

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

ExpandedBlockEnd.gif
         End Sub

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

InBlock.gif        
Dim BackColor As Integer
InBlock.gif        
Dim BackgroundImageStyle As String
InBlock.gif        
Dim ProperTab As New Hashtable
InBlock.gif        
Dim PropertyStr As String

InBlock.gif               dot.gifdot.gif
InBlock.gif        
With TA
InBlock.gif            ForeColor 
=
 .ForeColor.ToArgb
InBlock.gif            BackColor 
=
 .BackColor.ToArgb
InBlock.gif            BackgroundImageStyle 
=
 .BackgroundImageStyle.ToString
InBlock.gif            Indent 
=
 .Indent
InBlock.gif            NodeConnectorStyle 
=
 .NodeConnectorStyle.ToString
InBlock.gif            
If .Expand Then Expand = 1

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

InBlock.gif
InBlock.gif
InBlock.gif
Dim ProperTab as new HashTable
InBlock.gif             ProperTab.Add(
"ForeColor"
, ForeColor)
InBlock.gif            ProperTab.Add(
"BackColor"
, BackColor)
InBlock.gif            ProperTab.Add(
"BackgroundImageStyle"
, BackgroundImageStyle)
InBlock.gif            ProperTab.Add(
"Indent"
, Indent)
InBlock.gif      dot.gif.
InBlock.gif
InBlock.gif            
Try

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

InBlock.gif
                Dim InsertStr As String = "Insert into [TreeviewSetting] ([AppID],[PropName],[PropValue]) values (" & AppID & ",'" & PropertyStr & "','" & ProperTab.Item(PropertyStr).ToString.Replace("'""''"& "')"
InBlock.gif
                Dim UpdateStr As String = "Update [TreeviewSetting] set [PropValue]='" & ProperTab.Item(PropertyStr).ToString.Replace("'""''"& "' where [AppID]=" & AppID & " and [PropName]='" & PropertyStr & "'"
InBlock.gif
                Dim I As Integer
InBlock.gif
InBlock.gif                I 
= ExecuteScalar(ProviderType.SQL, CommandData.ConnectiongString, CommandType.Text, CommStr)
InBlock.gif                
If I = 0 Then

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

InBlock.gif            ProperTab 
= Nothing
InBlock.gif        
End Try
InBlock.gif
ExpandedBlockEnd.gif    
End Sub

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






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值