By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,761 Members | 1,183 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,761 IT Pros & Developers. It's quick & easy.

Help - User control and serialization

P: n/a
Tom
I am having a really annoying issue with serialization and a .NET User
Control I am writing. For example, let's say I have a couple of classes in
my control - first class is like:

Public Class User
Private _UserName As String
Private _UserCity As String
<System.ComponentModel.CategoryAttribute("User Info")> _
Public Property UserName() As String
Get
Return _UserName
End Get
Set(ByVal Value As String)
_UserName = Value
End Set
End Property
<System.ComponentModel.CategoryAttribute("User Info")> _
Public Property UserCity() As String
Get
Return _UserCity
End Get
Set(ByVal Value As String)
_UserCity = Value
End Set
End Property
End Class

Then the second class is a collection of the first, as so:

<Serializable()> _
Public Class UserCollection
Inherits Collections.CollectionBase

Default Public Property Item(ByVal index As Integer) As User
Get
Return CType(List(index), User)
End Get
Set(ByVal Value As User)
List(index) = Value
End Set
End Property

Public Function Add(ByVal value As User) As Integer
Return List.Add(value)
End Function
...
... (more functions follow, such as IndexOf, etc)
...
End Class

Then, in my .NET User Control, I have the following:

Private _UserFields As New UserCollection()
<System.ComponentModel.DesignerSerializationVisibi lity(ComponentModel.Design
erSerializationVisibility.Content), _
System.ComponentModel.Description("User Information"), _
System.ComponentModel.CategoryAttribute("User Info")> _
Public ReadOnly Property UserCriteria() As UserCollection
Get
Return _UserFields
End Get
End Property

In the solution I also have a test project that uses the control (just so I
can test it).

This seems to work fine as long as I am in the designer; i.e. I can use the
property pages to add user collection data (i.e. Name, City). I can close
Visual Studio and reopen the solution; the items I added are still there.
However, if I go to run the solution (by clicking on the run icon or
selecting Debug-Start or pressing F5) I end up with the following errors:

1. The test form that is hosting the control (form1.vb) goes white in the
designer and shows the following error message:

"An error occured while loading the document. Fix the error, and then try
loading the document again. The error message follows: The designer cannot
be modified at this time."

This occurs even though the form seems to execute fine and the application
shows up on the screen. I close the application and go back to the VB.NET
IDE, then close and reopen form1.vb in the designer (to get rid of the white
screen). Then the following build error shows up:

2. "At least one element in the source array could not be cast down to the
destination array type."

3. And then, when I use the property page and look at my control collection,
NOTHING is there anymore (i.e. the items I added before are all gone).

I have found VERY FEW articles or examples on making a control and using a
collection classe for property pages; the one I did find basically came up
with the same errors. It is very probable that I am missing something or
not doing something right (maybe I need to read the serialized data or
something when I actually execute the program??) But I am at a loss at this
point. If I can't figure this out I will just have to allow runtime only
modification of my control, which is something I really don't want to do.

Anyone know what I am missing here? Or know of a good example of this being
done in a user control? Thanks in advance....

Tom
Nov 20 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.