![]() |
Languages »
VB.NET »
HowTo
Intermediate
License: The Code Project Open License (CPOL)
Load and Save data using PropertyGridBy Rocco LabellarteA simple example of loading, saving and displaying objects in a PropertyGrid using serialization techniques |
VB, Windows, WinForms, Dev
|
||||||||
|
Advanced Search |
|
|
|
||||||||||||||||
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 web site 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.
The following example was developed and tested using Windows Vista and Visual Basic Express Edition 2008, running on the NET 3.5 network with Internet Explorer version 7. It should work with previous versions of these systems.
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 Property Grid:
Public Class Form1
'Load AppSettings
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()
' Actually change the form size
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 Buttons. 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 with 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:
Now switch to the code editor and change the Form1 code from:
Class Form End Class
to the following:
'============================================== 'How to serialize, load and save a class object '============================================== Imports System.Xml.Serialization Imports System.IO Public Class Form1 'Load AppSettings 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() ' Actually change the form size 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 ' Take the defaults fileNotFound = True End Try If fileNotFound Then retVal = New AppSettings retVal.WindowSize = New System.Drawing.Size(600, 600) Else 'Read it from the file 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
The above code sample is largely based on the response by Christian Lucht to a posting at: SDN Software Development Network
| You must Sign In to use this message board. | ||||||||
|
||||||||
|
||||||||
|
||||||||
|
||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 27 Jun 2008 Editor: |
Copyright 2008 by Rocco Labellarte Everything else Copyright © CodeProject, 1999-2009 Web19 | Advertise on the Code Project |