Declare a WithEvents class in Windows Form Designer which is not control base

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
I am making a TabStrip control to hold a number of tabs just like TabControl, but only with the tabs, no page. The control has a "Tabs Collection", which stores a list of "Tab" class. "Tab" is not inherited from Control, but each tab has its events. So when
the user add a "Tab" in the "Tabs Collection", each "Tab" instance should have a (Name) identifier, which user can access it in the code, and with WithEvents keyword so users can add event handlers to each "Tab". The expected out come should similar to this:

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Friend TabStrip1 <span style="color:Blue; As TabStrip
<span style="color:Blue; Friend <span style="color:Blue; WithEvents t1 <span style="color:Blue; As Tab
...
<span style="color:Blue; Me.TabStrip1.Tabs.Add(t1)
...
t1.Text = <span style="color:#A31515; "abcdefg"
...
<span style="color:Blue; Private <span style="color:Blue; Sub t1_TextChanged(...) <span style="color:Blue; Handles t1.TextChanged
[/code]

I dont know how to do this if "Tab" is not from Control class. I have tried to add "Tab"s in "Tabs Collection", all "Tab"s were there at design time (only), when I run the application, no "Tab"s was added.
Please could anyone help with my problem?
Here is a part of the code:

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Public <span style="color:Blue; Class TabStrip
<span style="color:Blue; Inherits Control
...
<span style="color:Blue; Public <span style="color:Blue; ReadOnly <span style="color:Blue; Property Tabs() <span style="color:Blue; As TabCollection
<span style="color:Blue; Get
<span style="color:Blue; Return _tabs
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; End <span style="color:Blue; Property
...
<span style="color:Blue; Public <span style="color:Blue; Class TabCollection
<span style="color:Blue; Inherits Generic.List(Of Tab)
<span style="color:Blue; Dim owner <span style="color:Blue; As TabStrip

<span style="color:Blue; Sub <span style="color:Blue; New(<span style="color:Blue; ByVal owner <span style="color:Blue; As TabStrip)
<span style="color:Blue; Me.owner = owner
<span style="color:Blue; End <span style="color:Blue; Sub

<span style="color:Blue; #Region <span style="color:#A31515; "Inherited from base class"
<span style="color:Blue; Overloads <span style="color:Blue; Sub Add(<span style="color:Blue; ByVal item <span style="color:Blue; As Tab)
...
<span style="color:Blue; End <span style="color:Blue; Sub
...
<span style="color:Blue; #End Region

<span style="color:Blue; End <span style="color:Blue; Class
<span style="color:Blue; End <span style="color:Blue; Class

<DefaultProperty(<span style="color:#A31515; "Text"), DefaultEvent(<span style="color:#A31515; "TextChanged"), Designer(<span style="color:Blue; GetType(TabDesigner)), TypeConverter(<span style="color:Blue; GetType(TabConverter))> _
<span style="color:Blue; Public <span style="color:Blue; Class Tab
<span style="color:Blue; Dim _text <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Dim _selected <span style="color:Blue; As <span style="color:Blue; Boolean
<span style="color:Blue; Dim _width <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Dim _autosize <span style="color:Blue; As <span style="color:Blue; Boolean = <span style="color:Blue; True
<span style="color:Blue; Dim _name <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Dim _x <span style="color:Blue; As <span style="color:Blue; Integer

<span style="color:Blue; Sub <span style="color:Blue; New(<span style="color:Blue; ByVal text <span style="color:Blue; As <span style="color:Blue; String)
<span style="color:Blue; Me.Text = text
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Sub <span style="color:Blue; New(<span style="color:Blue; ByVal text <span style="color:Blue; As <span style="color:Blue; String, <span style="color:Blue; ByVal width <span style="color:Blue; As <span style="color:Blue; Integer)
<span style="color:Blue; Me.Text = text
<span style="color:Blue; Me.AutoSize = <span style="color:Blue; False
<span style="color:Blue; Me.Width = width
<span style="color:Blue; End <span style="color:Blue; Sub

<span style="color:Blue; Public <span style="color:Blue; Property Name() <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Get
<span style="color:Blue; Return _name
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As <span style="color:Blue; String)
_name = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property

<span style="color:Blue; Property Text() <span style="color:Blue; As <span style="color:Blue; String
...
<span style="color:Blue; Public <span style="color:Blue; Event TextChanged(...)

<span style="color:Blue; Public <span style="color:Blue; ReadOnly <span style="color:Blue; Property Selected() <span style="color:Blue; As <span style="color:Blue; Boolean
...
<span style="color:Blue; Public <span style="color:Blue; Event SelectedChanged(...)
...
<span style="color:Blue; Public <span style="color:Blue; Property Width() <span style="color:Blue; As <span style="color:Blue; Integer
...
<span style="color:Blue; Public <span style="color:Blue; Event WidthChanged(...)
...
<span style="color:Blue; End <span style="color:Blue; Class

<span style="color:Blue; Public <span style="color:Blue; Class TabDesigner
<span style="color:Blue; Inherits ComponentDesigner

<span style="color:Blue; Public <span style="color:Blue; Overrides <span style="color:Blue; Sub Initialize(<span style="color:Blue; ByVal component <span style="color:Blue; As System.ComponentModel.IComponent)
<span style="color:Blue; MyBase.Initialize(component)
<span style="color:Blue; Dim service <span style="color:Blue; As IComponentChangeService = <span style="color:Blue; DirectCast(<span style="color:Blue; Me.GetService(<span style="color:Blue; GetType(IComponentChangeService)), IComponentChangeService)
<span style="color:Blue; AddHandler service.ComponentRename, <span style="color:Blue; AddressOf <span style="color:Blue; Me.OnComponentRename
<span style="color:Blue; End <span style="color:Blue; Sub

<span style="color:Blue; Private <span style="color:Blue; Sub OnComponentRename(<span style="color:Blue; ByVal sender <span style="color:Blue; As <span style="color:Blue; Object, <span style="color:Blue; ByVal e <span style="color:Blue; As ComponentRenameEventArgs)
<span style="color:Blue; Dim prop = <span style="color:Blue; Aggregate o <span style="color:Blue; As <span style="color:Blue; Object <span style="color:Blue; In TypeDescriptor.GetProperties(e.Component.<span style="color:Blue; GetType()) _
<span style="color:Blue; Let p = TryCast(o, PropertyDescriptor) _
<span style="color:Blue; Where p.Name = <span style="color:#A31515; "Name" <span style="color:Blue; AndAlso (<span style="color:Blue; Not p.DesignTimeOnly) _
<span style="color:Blue; Select p <span style="color:Blue; Into FirstOrDefault()
prop.SetValue(e.Component, e.NewName)
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; End <span style="color:Blue; Class

<span style="color:Blue; Public <span style="color:Blue; Class TabConverter
<span style="color:Blue; Inherits TypeConverter

<span style="color:Blue; Public <span style="color:Blue; Overrides <span style="color:Blue; Function CanConvertTo(<span style="color:Blue; ByVal context <span style="color:Blue; As ITypeDescriptorContext, <span style="color:Blue; ByVal destinationType <span style="color:Blue; As Type) <span style="color:Blue; As <span style="color:Blue; Boolean
<span style="color:Blue; Return (destinationType <span style="color:Blue; Is <span style="color:Blue; GetType(InstanceDescriptor) <span style="color:Blue; OrElse <span style="color:Blue; MyBase.CanConvertTo(context, destinationType))
<span style="color:Blue; End <span style="color:Blue; Function

<span style="color:Blue; Public <span style="color:Blue; Overrides <span style="color:Blue; Function ConvertTo(<span style="color:Blue; ByVal context <span style="color:Blue; As ITypeDescriptorContext, <span style="color:Blue; ByVal culture <span style="color:Blue; As Globalization.CultureInfo, <span style="color:Blue; ByVal value <span style="color:Blue; As <span style="color:Blue; Object, <span style="color:Blue; ByVal destinationType <span style="color:Blue; As Type) <span style="color:Blue; As <span style="color:Blue; Object
<span style="color:Blue; If (destinationType <span style="color:Blue; Is <span style="color:Blue; Nothing) <span style="color:Blue; Then
<span style="color:Blue; Throw <span style="color:Blue; New ArgumentNullException(<span style="color:#A31515; "destinationType")
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; If <span style="color:Blue; TypeOf value <span style="color:Blue; Is Tab <span style="color:Blue; Then
<span style="color:Blue; If (destinationType <span style="color:Blue; Is <span style="color:Blue; GetType(InstanceDescriptor)) <span style="color:Blue; Then
<span style="color:Blue; Dim member <span style="color:Blue; As MemberInfo = <span style="color:Blue; Nothing
<span style="color:Blue; Dim arguments <span style="color:Blue; As <span style="color:Blue; Object() = <span style="color:Blue; Nothing
<span style="color:Blue; Dim tab2 <span style="color:Blue; As Tab = <span style="color:Blue; DirectCast(value, Tab)
<span style="color:Blue; If tab2.AutoSize <span style="color:Blue; Then
member = <span style="color:Blue; GetType(Tab).GetConstructor(BindingFlags.CreateInstance, <span style="color:Blue; Nothing, <span style="color:Blue; New Type() {<span style="color:Blue; GetType(<span style="color:Blue; String)}, <span style="color:Blue; New ParameterModifier() {<span style="color:Blue; New ParameterModifier(1)})
arguments = <span style="color:Blue; New <span style="color:Blue; Object() {tab2.Text}
<span style="color:Blue; Else
member = <span style="color:Blue; GetType(Tab).GetConstructor(BindingFlags.CreateInstance, <span style="color:Blue; Nothing, <span style="color:Blue; New Type() {<span style="color:Blue; GetType(<span style="color:Blue; String), <span style="color:Blue; GetType(<span style="color:Blue; Integer)}, <span style="color:Blue; New ParameterModifier() {<span style="color:Blue; New ParameterModifier(1)})
arguments = <span style="color:Blue; New <span style="color:Blue; Object() {tab2.Text, tab2.Width}
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; If (<span style="color:Blue; Not member <span style="color:Blue; Is <span style="color:Blue; Nothing) <span style="color:Blue; Then
<span style="color:Blue; Return <span style="color:Blue; New InstanceDescriptor(member, arguments)
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; Return <span style="color:Blue; Nothing
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; Return <span style="color:Blue; MyBase.ConvertTo(context, culture, value, destinationType)
<span style="color:Blue; End <span style="color:Blue; Function

<span style="color:Blue; Public <span style="color:Blue; Overrides <span style="color:Blue; Function GetStandardValuesSupported(...) <span style="color:Blue; As <span style="color:Blue; Boolean
<span style="color:Blue; End <span style="color:Blue; Class
[/code]


View the full article
 
Back
Top