I have a usercontrol which exposes a property of a custom collection.
Here is the code used in the usercontrol.
Imports System.ComponentModel.DesignerSerializationVisibility
Public Class textbox
Inherits System.Windows.Forms.TextBox
Private _validation As New validationList
<System.ComponentModel.DesignerSerializationVisibility(Content)>
Public Property Validation As validationList
Get
Return _validation
End Get
Set(ByVal value As validationList)
_validation = value
End Set
End Property
End Class
Here is the collection class that this property uses.
Imports System.Collections.ObjectModel
<Serializable()> Public Class validationList
Inherits Collection(Of validationItem)
Public Shadows Sub Add(ByVal item As validationItem)
'//Check for duplicates
Dim dupe As Boolean = False
For n As Int32 = 0 To Items.Count - 1
If Items(n).Key = item.Key Then
dupe = True
Exit For
End If
Next
If dupe = False Then
Items.Add(item)
End If
End Sub
End Class
Here is the list of items that the collection class use
<Serializable()> Public Class validationItem
Private _key As validationTypes
Private _value As String
Public Sub New()
'//Empty constructor is needed for serialization
End Sub
Public Sub New(ByVal k As validationTypes, ByVal v As String)
_key = k
_value = v
End Sub
Public Enum validationTypes
Madatory = 1
[Integer] = 2
Numeric = 3
[Decimal] = 4
MaxValue = 5
MinValue = 6
MinLength = 7
Email = 8
End Enum
Public Property Value As String
Get
Return _value
End Get
Set(ByVal Value As String)
_value = Value
End Set
End Property
Public Property Key As validationTypes
Get
Return _key
End Get
Set(ByVal value As validationTypes)
_key = value
End Set
End Property
End Class
Here is what the designer code looks like after implementing the solution suggested by Pluntonix..
Dim ValidationItem1 As Testing_Project.validationItem = New Testing_Project.validationItem(Testing_Project.validationItem.validationTypes.MaxValue, "4")
Dim ValidationItem2 As Testing_Project.validationItem = New Testing_Project.validationItem(Testing_Project.validationItem.validationTypes.MinLength, "5")
Me.Textbox1.Validations.Add(ValidationItem1)
Me.Textbox1.Validations.Add(ValidationItem2)
I added a number of items to collection from the designer & I tried to retrieve them at runtime but all the keys are set to 0 and values are set to Nothing. I need the exact list of items added via the designer to be available as well, how can I make it work so that the actual values added via the designer exists at runtime as well.
A few changes to the UC property:
The item class is missing all the serialization support, these are the items that actually get serialized:
TRY this...it probably wont work, but it might. Using
DesignerSerializationVisibility.Visible>
might result in the Item values getting serialized. If not, (and even if it does) you will need aTypeConverter
.So you know, this is what we are talking about. In your Sub New for the form, drill into the InitializeComponent. The code for your validation items getting added to the collection will look like this:
VS needs help creating that code because it has no idea how to create a
FooBar
orValidationItem
to add to the collection.This is also a good way to examine how close you are to nailing the serialization requirements.
You probably will need a TypeConverter for VS to invoke to create your objects for the designer code. We need to return an
InstanceDescriptor
. This is code for one from something which does something similar to your collection which you should be able to adapt:<TypeConverter(GetType(ValidationItemConverter))>
to your ValidationItem class.Hidden
because we are doing them via the constructorNameSpace
wrapper if you end up with other things in the Lib which are not closely related.With a
TypeConverter
and the other stuff, you should be good to go. You will need to Build and Clean often. VS runs this code so you want to be sure that it is not using stale code or you end up chasing ghosts.The way this more or less works is that after adding some items via the Collection Editor, VS marks the form as dirty, rewrites the designer file (the code in InitializeComponent) then reloads the form (thats why it might flicker).
This in turn calls your class
Add
method which filters out the dupes. I think the Editor uses a temp copy of the collection while open so if you can CANCEL, it just returns the original version. So in the Editor, yourAdd
code doesn't run when you click the ADD button. This is why dupes arent filtered out in the editor.You
Add
does run when you close the editor and the form is rebuilt with the new designer code, but that means you will be retaining only the first instance usingIsFoo
and the others discarded. The way around this is a custom collection editor to poll the collection class to see if it is okay to add a newIsFoo
type to the collection.You will have to decide if a Custom Collection Editor is worth it or just saving the first instance of a validation rule is good enough.