Introduction
I've recently been researching the PropertyGrid
control in Visual Basic 2008. There is a lot of information out there, mainly for C# programmers. After quite a bit of research and the inevitable false starts, I came upon a response to a question on a website which looked like the answer to a common question: How do I load and save information into a PropertyGrid
using VB.NET? Unfortunately, the posting was a little cryptic. With a little tweaking, I came up with the following fully functioning example, and thought I'd share it with everyone.
Background
The following example was developed and tested using Windows Vista and Visual Basic Express Edition 2008, running on the .NET 3.5 Framework with Internet Explorer version 7. It should work with previous versions of these systems.
Using the Code
The example uses serialization to load and save a class object to and from an XML file. In order to achieve this, you simply add the attribute <Serialize()>
to the class identifier, as follows:
<Serializable()> _
Public Class AppSettings
You need to add two functions/methods to the class object you are defining which provide the load and save functionality. In the full example below, these are called:
Public Shared Function Load() As AppSettings
and
Public Sub Save()
You then simply include the following code in your main class to alternatively load or save the class object into your PropertyGrid
:
Public Class Form1
Dim _appSettings As New AppSettings()
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
_appSettings = AppSettings.Load()
Me.Size = _appSettings.WindowSize
PropertyGrid1.SelectedObject = _appSettings
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
_appSettings.Save()
End Sub
End Class
To run the complete example which follows, create a form with a PropertyGrid
and two Button
s. On clicking the LOAD button, we load an XML file with the window size saved (if the file already exists). If it doesn't already exist, the program sets the window width and height to 600 by 600. This information is then loaded into the PropertyGrid
. We can now modify the window size values in the PropertyGrid
. Clicking on the SAVE button serializes the information in the PropertyGrid
and saves the values to an XML file on disk. If you now close the program and then re-run it, when you click on the LOAD button, the values you previously entered into the PropertyGrid
are re-loaded and displayed.
Follow these steps to create the example:
- Create a new project in VB.NET 2008
- Display your form (called
Form1
) in Design mode - Add a
PropertyGrid
control - Add a
Button
control and change the Text
property to LOAD - Add a second
Button
control and change the Text
property to SAVE
Now, switch to the code editor and change the Form1
code from:
Class Form
End Class
to the following:
Imports System.Xml.Serialization
Imports System.IO
Public Class Form1
Dim _appSettings As New AppSettings()
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
_appSettings = AppSettings.Load()
Me.Size = _appSettings.WindowSize
PropertyGrid1.SelectedObject = _appSettings
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
_appSettings.Save()
End Sub
End Class
<Serializable()> _
Public Class AppSettings
Protected _size As System.Drawing.Size
Public Property WindowSize() As System.Drawing.Size
Get
Return _size
End Get
Set(ByVal value As System.Drawing.Size)
_size = value
End Set
End Property
Public Shared Function Load() As AppSettings
Dim serializer As XmlSerializer = New XmlSerializer(GetType(AppSettings))
Dim retVal As AppSettings
Dim reader As TextReader
Dim fileNotFound As Boolean
Try
reader = New StreamReader("MyAppSettings.xml")
Catch ex As FileNotFoundException
fileNotFound = True
End Try
If fileNotFound Then
retVal = New AppSettings
retVal.WindowSize = New System.Drawing.Size(600, 600)
Else
retVal = serializer.Deserialize(reader)
reader.Close()
End If
Return retVal
End Function
Public Sub Save()
Dim serializer As New XmlSerializer(GetType(AppSettings))
Dim writer As TextWriter = New StreamWriter("MyAppSettings.xml")
serializer.Serialize(writer, Me)
writer.Close()
End Sub
End Class
Points of Interest
The above code sample is largely based on the response by Christian Lucht to a posting at: SDN Software Development Network.