How?:: Creating objects with design-time editable subobject collections

lickuid

Active member
Joined
Nov 6, 2006
Messages
37
Programming Experience
Beginner
I'm not sure if the title was descriptive enough, but I'll explain.
I created 2 objects. 1 Is a container, the 2nd is a tab object... I want to be able to load tabs onto the container at runtime, but more and most importantly, during build time. (Note* Though I use the word Tabs, i'm not building a tab control, it's more like a task list with multiple fields) I figured I'd do something like:
VB.NET:
[COLOR=SeaGreen]'In the container Class...[/COLOR]
    [COLOR=RoyalBlue]Public [/COLOR]Tabs [COLOR=RoyalBlue]As [/COLOR]Collection
    [COLOR=RoyalBlue]Property [/COLOR]TabCollection() [COLOR=RoyalBlue]As [/COLOR]Collection
        [COLOR=RoyalBlue]Get[/COLOR]
            [COLOR=RoyalBlue]Return [/COLOR]Tabs
        [COLOR=RoyalBlue]End Get[/COLOR]
        [COLOR=RoyalBlue]Set[/COLOR]([COLOR=RoyalBlue]ByVal [/COLOR]value [COLOR=RoyalBlue]As [/COLOR]Collection)
            Tabs = value
        [COLOR=RoyalBlue]End Set
    End Property
[/COLOR]
A few questions with this would arise... Tabs as Collection.... of what? Well, I would like to tell vb that it's a collection of to-be-created qltabs - the custom class I wrote for tabs...I'm very excited to find the solution to this because it would answer so many other questions I have.

It works, but needless to say, not as intended... It opens up the collection editor while in the properties panel, but at design time, I want to be able to use the Collection Editor to add tabs to the contianer. Also, is the a way to have the context menu show with the option to edit the collection of a custom control when you right click on a control like the tree view for example?

I read a tutorial that wasn't helpful and it seems like this is somewhat of a drawn-out process. But could someone set me in the right direction?? Thanks.
 
Right, first of all forget about the collection. Create your own strongly typed collection that inherits from collectionbase (if you need more info on this then ask) or use the generics if your on .Net 2.0, Second if you want to get into it i'll show you how to add designerverbs to your control that will allow you to add your tabs directly from the control with out using the properties editor, and how to use the designer services to interact with your control at design time.
 
Thanks vis781, I would appreciate that help greatly...
I came across this link in a google search... judging by what you know, would this be a tutorial that could would show me how to add designerverbs and how to use designer services?
 
That link for 'collection controls with rich design time support' is not a good example of how to create designers in addition that article was written for VS.Net 2003 and some of the info in there is out of date and some of the methods in there have been depreciated in the newer version. They are without doubt quite a well kept secret in the world of component development.

lickuid, adding the desingerverbs is relatively easy. To start, depending on whether your new component inherits from control or component will dertemine the type of designer you want. In your case you will need to add a designer to your tab control. Add a new class to your project and add

VB.NET:
Inherits ControlDesigner

We'll start by adding a simple verb that doesn't do anything just yet. Override the read only designer verb property and add the following.

VB.NET:
Dim Myverbs as new DesingerVerbCollection
MyVerbs.Add("Add Tab", AddressOf Me.OnAddTab)
Return MyVerbs


add this to your class

VB.NET:
Private Sub OnAddTab(Byval Sender as Object, Byval e as Eventargs)
End Sub

Congratulations, you've just added your first verb to yuor control. But now you have to associate your new designer with your control. You do that by adding the designer attribute to the very top of your class.

VB.NET:
<Designer(.....)> _
Public Class


Most of this kind of stuff is in the articles JohnH posted, but do not follow it when it comes to using the Designer Services as it is a bit mis-leading. If you've done all that correct when you click on yuor control at design time a dotted line should appear round your control and also a little arrow in the top right hand corner. Click this arrow to expnd the box, and in there should be your new custom verb that doesn't do anything yet.
 
Thanks Vis.. Okay, that actually does seem pretty simple. But in order to get the control collection to show up via a designverb, I'm all question marks because I have to create a typeconverter and all the tutorials I've come across don't really explain that in adequate detail. But I'm far from giving up. Ultimately, I want to build a control that keeps sub-controls in a collection but has design-time as well as runtime capabilities.
Thanks again for all the help. You guys are the greatest! :D
 
Here's a simple typeconverter for you...

Public Class lickuid'sTypeConverter
Inherits TypeConverter

VB.NET:
Public Overloads Overrides Function CanConvertFrom(ByVal context As ITypeDescriptorContext, ByVal sourceType As System.Type) As Boolean      
If (sourceType Is GetType(String)) Then            
Return True      
End If      
Return MyBase.CanConvertFrom(context, sourceType)
End Function
 
Public Overloads Overrides Function CanConvertTo(ByVal context As ITypeDescriptorContext, ByVal destinationType As System.Type) As Boolean   
If (destinationType Is GetType(InstanceDescriptor)) Then           
Return True      
End If      
Return MyBase.CanConvertTo(context, destinationType)
End Function

To apply this to your class use the typeconverter attribute

VB.NET:
<TypeConverter(GetType(lickuid'sTypeConverter))> _
Public Class...
 
I still don't understand it, but I really didn't get much of a chance to try it today. But hopefully, through practice, I can gain an understanding of it.
I can't thank you enough...Thanks!
 
Okay, I'm at a loss, I've tried to create my controldesigner but after i type inherits, intellisense won't show ControlDesigner as an option. I did imports system.windows.forms.design as well... Why might this be happening?
 
Back
Top