Here's a class I wrote a while ago that uses binary serialization and reflection to serialize all the serializable properties of a form and its child controls to a given stream, this can be any type of stream, such as a filestream or a networkstream.
It can deserialize from a given stream and apply the property values directly to a form (and the child controls).
Personally I havent had any use of it but I figured someone might find it interesting...I'll attach it to this post.
The loading can be sluggish if there are alot of data to read in, altough I am working on making the LoadLayout method asynchronous.
I havent commented the code in any way unfortunately, so dont hesitate to ask if something looks strange.
Feel free to comment.
EDIT: Using the class is pretty straight forward, but I figured I'd add an example:
To save the forms layout to a file, create a filestream and pass it + the form instance to the SaveLayout method:
The SaveLayout and LoadLayout methods are both shared so you dont need to create a new instance of the class in order to use them.
VB.Net Code:
Dim fs As New IO.FileStream("c:\layout.bin", IO.FileMode.Create)
LayoutSerializer.SaveLayout(Me, fs)
fs.Close()
And here's how you'd load it again:
VB.Net Code:
Dim fs As New IO.FileStream("c:\layout.bin", IO.FileMode.Open)
Note that I've specified "DontCreateExistingControls" in the LoadLayout call..
This means that controls that already exist on the form should not be created again, but its property values will still be overwritten with the property values from the loaded layout.
There are two more LoadMode enums, I think you understand what they do by looking at their names.
Last edited by Atheist; Feb 4th, 2008 at 01:16 PM.
Also, using the latest discovery of mine (see my last codebank submission), I'll try to see if i can retain the eventhandlers for the controls events after serialization/deserialization.
Im currently working on it, serializing information about the eventhandlers wasnt as easy as I thought, my first attempt was to immediately serialize the Delegate object that represents the EventHandler for each event, but the problem is that the eventhandler has a property called Target that references the object whos event it is handling, it will try to serialize that object aswell, which wont work well as most (all?) controls arent serializable.
My second immediate thought was to just serialize the delegate information as a System.Reflection.MethodInfo object, this works well...but upon deserializing i must reconstruct a delegate using this MethodInfo object and this is where I am at the moment...but I think I'll figure it out soon.
I figured I wont start with the asynchronous methods until the synchronous methods are 100% complete.
I am trying to use the LayoutSerializer but I am having an issue with it. On line 79 of the Serializer in the ControlInfo Class I keep getting a ArgumentException was unhandled error. That reads "An item with the same key has already been added."
It appears it has something to do with adding a value with the key name of "Padding" because the key already exists.
Not quite sure how to fix this.
Thanks.
"Don't try to be a great man. Just be a man and let history make its own judgement."
Private Class ControlInfo
Public ControlType As Type
Public Properties As New Dictionary(Of String, Object)
Sub New(ByVal ctrl As Control)
ControlType = ctrl.GetType
Dim value As Object
For Each pi As Reflection.PropertyInfo In ControlType.GetProperties
If pi.CanWrite Then
----------->>>>> value = pi.GetValue(ctrl, Nothing)
If value Is Nothing OrElse value.GetType.IsSerializable Then
Properties.Add(pi.Name, value)
End If
End If
Next
End Sub
End Class
Description is "Exception has been thrown by the target of an invocation."
when I try to apply
Code:
Dim fs As New IO.FileStream("c:\layout.bin", IO.FileMode.Create)
LayoutSerializer.SaveLayout(Me, fs)
fs.Close()
Private Class ControlInfo
Public ControlType As Type
Public Properties As New Dictionary(Of String, Object)
Sub New(ByVal ctrl As Control)
ControlType = ctrl.GetType
Dim value As Object
For Each pi As Reflection.PropertyInfo In ControlType.GetProperties
If pi.CanWrite Then
----------->>>>> value = pi.GetValue(ctrl, Nothing)
If value Is Nothing OrElse value.GetType.IsSerializable Then
Properties.Add(pi.Name, value)
End If
End If
Next
End Sub
End Class
Description is "Exception has been thrown by the target of an invocation."
when I try to apply
Code:
Dim fs As New IO.FileStream("c:\layout.bin", IO.FileMode.Create)
LayoutSerializer.SaveLayout(Me, fs)
fs.Close()
on my form closed event...
it only appear the error when my form contain listview, dataGridView....
any solution to solve it??
Last edited by wenight; Aug 26th, 2009 at 10:45 PM.
I don't know what the value it get when happening...
How to prevent unnecessary property to store??
Something like
Code:
If control= "Textbox" Then
If property <> "Text" Then
store to binary...
End if
Else If control = "Label" Then
If property <> "Text" Then
store to binary...
End If
Else if control = "ListView" Then
If property <> "Item" or property <> "Column" Then
store to binary...
End If
Else if control = "Checkbox" Then
If property <> "Checked" Then
store to binary...
End If
End if
Last edited by wenight; Aug 27th, 2009 at 07:10 AM.
I like your class very much; it is so easy to save and load a Form's layout! Thanks.
Though, I typed something in a Textbox and used your class to save it, but when loading it, the Textbox didn't have that text, or does your code not pretend to do so?
Last edited by pimvdb; Aug 27th, 2009 at 03:05 PM.
System.Reflection.TargetInvocationException was unhandled Message="Exception has been thrown by the target of an invocation." Source="mscorlib"
So, since a blank project worked fine and the error seem to target the ControlType, I started another blank project with all of the same controls. I have some custom made controls and thought that was the issue to begin with.
With the process of elimination, I determined that the error was caused because of a ToolStrip. Imagine that.
Is this really of any use to anyone? If so, in what context are you using it?
Looking back at this submission of mine, I almost wish I could remove it from the codebank as its just plain worthless
Is this really of any use to anyone? If so, in what context are you using it?
Looking back at this submission of mine, I almost wish I could remove it from the codebank as its just plain worthless
It's not worthless. In fact, I love it. I'm having some trouble getting it to work with the ToolStrip mentioned above and some custom controls I made, but other than that, I love the idea.
I'm currently working on a project that requires my 'designer' to be saved. It's kind of the same fashion that we use in VS. And something like this would make my life so much easier.
The problem I have with my custom controls, is that everything appears to save and load, but when the custom controls load, they're text do not. They're custom labels inside of a custom panel =/