Results 1 to 6 of 6

Thread: [RESOLVED] Custom Control 'Serializable' Problem

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Huntsville, AL
    Posts
    62

    Resolved [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.

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Custom Control 'Serializable' Problem

    For a direction look at http://www.vbforums.com/showthread.php?t=599375 by NickThissen which should help.

  3. #3
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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:
    1. Public Class PanelContainer
    2.  
    3.     Public Sub New()
    4.  
    5.         ' This call is required by the designer.
    6.         InitializeComponent()
    7.  
    8.         ' Add any initialization after the InitializeComponent() call.
    9.  
    10.         ' Create a new PanelCollection and pass the current PanelContainer instance along
    11.         _Panels = New PanelCollection(Me)
    12.  
    13.        
    14.     End Sub
    15.  
    16.     Private _Panels As PanelCollection
    17.     Public ReadOnly Property Panels() As PanelCollection
    18.         Get
    19.             Return _Panels
    20.         End Get
    21.     End Property
    22.  
    23.     Public Class PanelCollection
    24.         Inherits System.Collections.ObjectModel.Collection(Of Panel)
    25.  
    26.         Private container As PanelContainer
    27.  
    28.         Public Sub New(ByVal parent As PanelContainer)
    29.             container = parent
    30.         End Sub
    31.  
    32.         Protected Overrides Sub InsertItem(ByVal index As Integer, ByVal item As System.Windows.Forms.Panel)
    33.             MyBase.InsertItem(index, item)
    34.  
    35.             ' Also add the panel to the Controls collection
    36.             container.Controls.Add(item)
    37.  
    38.             ' And give it a random backcolor, just for testing purposes so we can see it in the designer easily
    39.             Dim rnd As New Random
    40.             item.BackColor = Color.FromArgb(rnd.Next(0, 256), _
    41.                                           rnd.Next(0, 256), _
    42.                                           rnd.Next(0, 256))
    43.         End Sub
    44.  
    45.         Protected Overrides Sub RemoveItem(ByVal index As Integer)
    46.             ' Also remove the panel from the Controls collection
    47.             Dim panel = Me.Item(index)
    48.             container.Controls.Remove(panel)
    49.  
    50.             MyBase.RemoveItem(index)
    51.         End Sub
    52.  
    53.     End Class
    54.  
    55. 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).

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Huntsville, AL
    Posts
    62

    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.

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Huntsville, AL
    Posts
    62

    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.

  6. #6
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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
  •  



Click Here to Expand Forum to Full Width