Author:水如烟
在这是第三次说配置文件,因为有了泛型,所以现在做的也更简单,使用也更方便了。
示例代码:
Public
Class
MainForm
Private gConfigManager As New LzmTW.ConfigManger( Of Student)
Private Sub MainForm_Load( ByVal sender As Object , ByVal e As System.EventArgs) Handles Me .Load
Me .PropertyGrid1.SelectedObject = Me .gConfigManager
End Sub
Private Sub ReadButton_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReadButton.Click
gConfigManager.Read()
End Sub
Private Sub SaveButton_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
gConfigManager.Save()
End Sub
Private Sub DataGridButton_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridButton.Click
Dim table As DataTable = LzmTW.uSystem.uReflection.DatasFunction.CreateTableFromType( GetType (Student))
With gConfigManager.GetEnumerator
While .MoveNext
LzmTW.uSystem.uReflection.DatasFunction.ItemAppendToTable( Of Student)(.Current, table)
End While
End With
table.AcceptChanges()
Me .DataGridView1.DataSource = table
End Sub
Private Sub OutputNameButton_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OutputNameButton.Click
Me .gConfigManager.ForEach( AddressOf printName)
End Sub
Private Sub printName( ByVal item As Student)
Console.WriteLine(item.Name)
End Sub
Private Sub FindNameButton_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindNameButton.Click
Console.WriteLine( Me .gConfigManager.Find( " Name " , " 张三 " ).NO)
End Sub
End Class
Private gConfigManager As New LzmTW.ConfigManger( Of Student)
Private Sub MainForm_Load( ByVal sender As Object , ByVal e As System.EventArgs) Handles Me .Load
Me .PropertyGrid1.SelectedObject = Me .gConfigManager
End Sub
Private Sub ReadButton_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReadButton.Click
gConfigManager.Read()
End Sub
Private Sub SaveButton_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
gConfigManager.Save()
End Sub
Private Sub DataGridButton_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridButton.Click
Dim table As DataTable = LzmTW.uSystem.uReflection.DatasFunction.CreateTableFromType( GetType (Student))
With gConfigManager.GetEnumerator
While .MoveNext
LzmTW.uSystem.uReflection.DatasFunction.ItemAppendToTable( Of Student)(.Current, table)
End While
End With
table.AcceptChanges()
Me .DataGridView1.DataSource = table
End Sub
Private Sub OutputNameButton_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OutputNameButton.Click
Me .gConfigManager.ForEach( AddressOf printName)
End Sub
Private Sub printName( ByVal item As Student)
Console.WriteLine(item.Name)
End Sub
Private Sub FindNameButton_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindNameButton.Click
Console.WriteLine( Me .gConfigManager.Find( " Name " , " 张三 " ).NO)
End Sub
End Class
<
Serializable()
>
_
Public Class Student
Private gName As String
Private gNO As String
Private gBirthday As Date
Public Property Name() As String
Get
Return gName
End Get
Set ( ByVal value As String )
gName = value
End Set
End Property
Public Property NO() As String
Get
Return gNO
End Get
Set ( ByVal value As String )
gNO = value
End Set
End Property
Public Property Birthday() As Date
Get
Return gBirthday
End Get
Set ( ByVal value As Date )
gBirthday = value
End Set
End Property
End Class
Public Class Student
Private gName As String
Private gNO As String
Private gBirthday As Date
Public Property Name() As String
Get
Return gName
End Get
Set ( ByVal value As String )
gName = value
End Set
End Property
Public Property NO() As String
Get
Return gNO
End Get
Set ( ByVal value As String )
gNO = value
End Set
End Property
Public Property Birthday() As Date
Get
Return gBirthday
End Get
Set ( ByVal value As Date )
gBirthday = value
End Set
End Property
End Class
步骤说明:
第一次运行时数据集是空的,所以要在PropertyGrid的Collection项目上添加项目。之后保存文件。
下次运行时,可以读文件了。
效果图解: