Collection editor problem

NicoVB

Centurion
Joined
Jan 2, 2002
Messages
160
Location
Belgium
I have created a new component: an IconList


Code:
Public Class IconList
	Inherits System.ComponentModel.Component
	Private mnIcons As Icons = New Icons()
	Public Property Icons() As Icons
		Get
			Return mnIcons
		End Get
		Set(ByVal Value As Icons)
			mnIcons = Value
		End Set
	End Property
End Class

Public Class Icons
	Inherits System.Collections.CollectionBase

	Public Sub Add(ByVal ico As Icon)
		List.Add(ico)
	End Sub
	Public Sub New()
		MyBase.New()
	End Sub
	Public Sub Remove(ByVal index As Integer)
		If index > Count - 1 Or index < 0 Then
			MsgBox("Index not valid!")
		Else
			List.RemoveAt(index)
		End If
	End Sub
	Public ReadOnly Property Item(ByVal index As Integer) As Icon
		Get
			Return CType(List.Item(index), Icon)
		End Get
	End Property
End Class




But when I try to add an item in the collection editor the editor gives this error message:

Constructor on type System.Drawing.Icon not found


Thanks in advance
 
It is trying to create an instance of the Icon class to add to your collection, but it can't. I suggest you create your own class, with an Icon property, and make your collection a collection of that class.
 
So I have to build a new class clsIcon with Icon as a roperty in it. And then I have to write:


Code:
Public Class Icons
	Inherits System.Collections.CollectionBase

	Public Sub Add(ByVal ico As Icon)
		List.Add(ico)
	End Sub
	Public Sub New()
		MyBase.New()
	End Sub
	Public Sub Remove(ByVal index As Integer)
		If index > Count - 1 Or index < 0 Then
			MsgBox("Index not valid!")
		Else
			List.RemoveAt(index)
		End If
	End Sub
	Public ReadOnly Property Item(ByVal index As Integer) As Icon
		Get
			Return CType(List.Item(index), Icon)
		End Get
	End Property
End Class


????
 
I have now done this changes, but there are other problems now.

It's very strange... but there is NO CODE GENERATED in the 'Windows Form Designer generated code' region for me when I add a few IconAd elements.

And when I set the property Icon from a IconAd element, it doesn't fill in that property.


What is the problem here???
 
That new code you pasted isn't correct, I don't know if you've written more since then. Suggest you remove your Remove method for the time being, the designer doesn't need it. Also, there's no point declaring a sub new that just calls the base constructor, that's what it would do anyway.

You have to make your collection accept and return items of your new class (IconAd was it) instead of icon.

Getting the designer to generate code for your new class is a little tricky, but I wrote all about it in my tutorial in the tutor's corner entitled "Authoring Advanced Windows Forms Controls".
 
Sorry, indeed my code was wrong. I've allready changed that code. Now i've removed the Sub New() also.


And is it so difficult to do this? Is there no simplier way to build an IconList just the same like an ImageList????

I will try to read your tutorial.
 
The ImageList has a custom collection editor dialog, which I have no experience in developing.

If you want the designers to serialize your class, you have to write a custom type convertor for it, which I showed how to do in my tutorial.
 
Another problem occured now. So the collection problem is OK. But I have for each IconAd element a Icon property. But if I want to fill in that Icon property, he just fills it as NOTHING. So why is that? (Also if I add a property 'temp' as String for example.)

it creates this code:
----------------------------

Code:
IconAd1.Icon = Nothing
		IconAd2.Icon = Nothing
		Me.IconList1.Icons.Add(IconAd1)
		Me.IconList1.Icons.Add(IconAd2)
 
Last edited:
I don't know how to tell the serializers to persist icons in the binary resource files, no.

Try a google search for something like that. You want your class member to be serialized in the same manner as the .Icon property is for a form.
 
Sorry, but i made a very stupid mistake in the property statement. So you don't have to solve that problem


BUT!!!!

There is another LITTLE problem that has to be solved:
-----------------------------------------------------------------------
When you set the property of ANOTHER control WITHOUT a collection on an icon. But you decides that you don't want to have an icon displayed on any sort of box. And you RIGHT click on the property like on the image property of a picturebox, you can see a menu with two options : RESET and DESCRIPTION.

But when I right click on the icon property of my custom control, i can see only DESCRIPTION in that menu.

This is a small problem. But it can be user-friendly that get rid of this problem.


Thanks if you can solve this problem too. :D
 
I didn't even know you did this, but I'd guess it would be by applying a DefaultValueAttribute to that member, passing Nothing.
 
I think I have found another solution. I read it in my book:

'Providing a Reset Method for a Control Property'
------------------------------------------------------------------
Code:
Public Sub ResetMyProperty()
mnMyProperty = mnMyPropertyDefaultValue
Me.Invalidate()


It works.


But many thanks for incredibily helping me and I now have written two new controls.
 
Back
Top