-
Nov 29th, 2010, 03:40 PM
#1
Thread Starter
Member
[RESOLVED] Custom Control 'Serializable' Problem
Hello,
I'm trying to create a custom control that contains a List(Of Panel). I quickly ran into an error about the base class Panel being nonserializable. So I created my own class that inherits from Panel and implemented the ISerializable interface. At the surface, everything appears to work, at least everything at design time. I don't know what would happen if I tried to run the application.
Anyway, the errors occur when I try to open a file that contains my custom control. For example, I create a new Windows Form and add my custom control to it. I save the Form and close it. Then I try to open the form and get the following error:
Code:
Object of type 'MyTestApplication.SerializablePanel[]' cannot be converted to type 'MyTestApplication.SerializablePanel[]'.
I don't know what I'm doing wrong? I've opened the XML file to see what it's actually writing, and read the header notes about using binary base64 serialization. I think that's what I'm using, but I'm not sure.
Below is the code for the class SerializablePanel
Code:
Imports System.Runtime.Serialization
Imports System.ComponentModel
<Serializable()> _
Public Class SerializablePanel
Inherits Panel
Implements ISerializable
#Region "Initializers"
Public Sub New()
End Sub
#End Region
#Region "Properties"
Private _Text As String = Nothing
<Browsable(True)> _
Public Overrides Property Text() As String
Get
Return _Text
End Get
Set(ByVal value As String)
_Text = value
End Set
End Property
#End Region
#Region "Serialization"
Public Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
_Text = info.GetString("Text")
End Sub
Public Sub GetObjectData(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext) Implements System.Runtime.Serialization.ISerializable.GetObjectData
info.AddValue("Text", _Text)
End Sub
#End Region
End Class
Hopefully, my code isn't too bad, I'm just a hobby coder.
-
Nov 29th, 2010, 05:18 PM
#2
Re: Custom Control 'Serializable' Problem
For a direction look at http://www.vbforums.com/showthread.php?t=599375 by NickThissen which should help.
-
Nov 29th, 2010, 06:12 PM
#3
Re: Custom Control 'Serializable' Problem
What do you want to do with your panels? Should the panels actually be added to your UserControl (or just remain in memory?), Should they be visible during design-time? Should they be editable during design-time? Should the user be able to add controls to them during design-time?
Here is the most simple implementation that I can think of. The main point is that I use a custom collection (PanelCollection) instead of a List(Of Panel). In there, I add/remove the panels to the UserControl as well when they are added/removed from the collection.
vb.net Code:
Public Class PanelContainer Public Sub New() ' This call is required by the designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. ' Create a new PanelCollection and pass the current PanelContainer instance along _Panels = New PanelCollection(Me) End Sub Private _Panels As PanelCollection Public ReadOnly Property Panels() As PanelCollection Get Return _Panels End Get End Property Public Class PanelCollection Inherits System.Collections.ObjectModel.Collection(Of Panel) Private container As PanelContainer Public Sub New(ByVal parent As PanelContainer) container = parent End Sub Protected Overrides Sub InsertItem(ByVal index As Integer, ByVal item As System.Windows.Forms.Panel) MyBase.InsertItem(index, item) ' Also add the panel to the Controls collection container.Controls.Add(item) ' And give it a random backcolor, just for testing purposes so we can see it in the designer easily Dim rnd As New Random item.BackColor = Color.FromArgb(rnd.Next(0, 256), _ rnd.Next(0, 256), _ rnd.Next(0, 256)) End Sub Protected Overrides Sub RemoveItem(ByVal index As Integer) ' Also remove the panel from the Controls collection Dim panel = Me.Item(index) container.Controls.Remove(panel) MyBase.RemoveItem(index) End Sub End Class End Class
(PanelContainer is a new UserControl).
This way you can add panels during design-time and they are completely editable. I must admit I didn't expect that, I thought they wouldn't be. I also thought you'd need the DesignerSerializationVisibility attribute, but again, I was wrong. I have a reasonable amount of experience with design-time support like this but as you can see I still don't know everything
If you're getting into design-time support I suggest you take a look at my Wizard Usercontrol and my Outlook NavigationBar. Those both have panels (actually: controls inheriting Panel) that are added during design-time and are made editable to a certain extent (more or less like TabPages).
-
Nov 30th, 2010, 07:46 AM
#4
Thread Starter
Member
Re: Custom Control 'Serializable' Problem
Wow! This all looks great! I'll need some time to process/digest it. I'll post back in a little bit with my results.
-
Dec 4th, 2010, 02:31 PM
#5
Thread Starter
Member
Re: Custom Control 'Serializable' Problem
Okay, basically, a while ago I had wanted a better tab control. My hacked together solution was to use the existing tab control but create a custom label for each tab "button". Then depending on which label was clicked, the appropriate tabpage would be selected. Also, when the form is loaded, the tab control is moved and resized such that the tab buttons are outside the visible bounds of the containing object.
The reason I started this thread was because I wanted to venture into making my own custom tab control with better button designs and such.
I've looked at the Wizard Control by NickThissen and that gave me a great start into a custom tab control that has really polished design-time features. I will try to use those practices for what I'm working on. Thanks!
EDIT
I'd rate your post NickThissen, but apparently you're the only member I rate, so the forum won't let me.
-
Dec 4th, 2010, 02:37 PM
#6
Re: [RESOLVED] Custom Control 'Serializable' Problem
If you're looking to customize the TabControl there's way easier solutions. By customize I mean custom drawing it. You can basically draw everthing as long as you know where and when to draw it. If that's what you are looking for let me know, I can send you a VS2010 style TabControl so you can take a look. In the meanwhile, have a look here, there's lots of stuff about custom drawing tabcontrols and also a lot of design-time solutions. For example, the PanelManager on that website was the base for my WizardControl.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|