| Yes, you can extend properties of controls or components.
|
| > Is it possible to add an extended property to components like I can with controls via
| > IExtenderProvider?
| >
| > JackRazz
JackRazz, to prove that this is possible, I've extended the Timer
component to include an automatic shutoff after a certain number of
times called. Take a look to see how I did it.
Option Explicit On
Option Strict On
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Timers
<ProvideProperty("AutoShutOff", GetType(Timers.Timer)), _
ProvideProperty("ShutOffValue", GetType(Timers.Timer))> _
Public Class TimerExtender
Inherits System.ComponentModel.Component
Implements IExtenderProvider
Friend ht As New Hashtable()
Dim value As Integer
#Region " Component Designer generated code "
Public Sub New(ByVal Container As System.ComponentModel.IContainer)
MyClass.New()
'Required for Windows.Forms Class Composition Designer support
Container.Add(Me)
End Sub
Public Sub New()
MyBase.New()
'This call is required by the Component Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Component overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Component Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Component Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
components = New System.ComponentModel.Container()
End Sub
#End Region
Public Function CanExtend(ByVal extendee As Object) As Boolean
Implements System.ComponentModel.IExtenderProvider.CanExtend
If TypeOf extendee Is Timers.Timer Then
AddPropertyValue(DirectCast(extendee, Timers.Timer))
Return True
Else
Return False
End If
End Function
Private Function AddPropertyValue(ByVal ctrl As Timers.Timer) As
TimerExtendedProperties
If ht.Contains(ctrl) Then
Return CType(ht(ctrl), TimerExtendedProperties)
Else
Dim tbProperties As New TimerExtendedProperties()
ht.Add(ctrl, tbProperties)
AddHandler ctrl.Elapsed, AddressOf TimerElapsed
Return tbProperties
End If
End Function
Private Sub TimerElapsed(ByVal sender As System.Object, ByVal e As
System.Timers.ElapsedEventArgs)
Dim ctrl As Timers.Timer = CType(sender, Timers.Timer)
value += 1
If value >= GetShutOffValue(ctrl) Then
ctrl.Enabled = False
value = 0
End If
End Sub
<Description("Enable automatic shutoff?"), _
DefaultValue(False)> _
Public Function GetAutoShutOff(ByVal ctrl As Timers.Timer) As Boolean
If ht.Contains(ctrl) Then
Return CType(ht(ctrl), TimerExtendedProperties).enabled
Else
Return False
End If
End Function
Public Sub SetAutoShutOff(ByVal ctrl As Timers.Timer, ByVal value
As Boolean)
AddPropertyValue(ctrl).enabled = value
End Sub
<Description("Value of when to shutoff timer."), _
DefaultValue(1)> _
Public Function GetShutOffValue(ByVal ctrl As Timers.Timer)As Integer
If ht.Contains(ctrl) Then
Return CType(ht(ctrl), TimerExtendedProperties).count
Else
Return 0
End If
End Function
Public Sub SetShutOffValue(ByVal ctrl As Timers.Timer, ByVal value
As Integer)
AddPropertyValue(ctrl).count = value
End Sub
End Class
Public Class TimerExtendedProperties
Public enabled As Boolean = False
Public count As Integer = 1
End Class
JackRazz - 09 Apr 2004 20:15 GMT
Yea Right,
Sorry, I havn't checked back on this in a while as I resolved it. My mistake that
first time was that I copied/pasted extender code from a control and forgot to change
the ProvideProperty.
I had a bit of figuring to do in order for the extended class to handle both
components and controls. After a bit of experimenting, I finally figured out that an
extender can handle both controls and components by simply using GetType(Components)
in the ProvideProperty attribute. I'm guessing this is because controls are
inherited from components.
On another note, I was trying to figure out how to get a components project name
during design time. I just ran across this snippet in a CodeProject example:
private static Object getProject()
{
return
Connect.Application.ActiveDocument.ProjectItem.ContainingProject.FileName;
}
It looks like all I need a simple Connect class and I'm in business<grin>. Its
sometimes amazing how I learn something new about the Framework on a daily basis.
Thanks - JackRazz
| JackRazz, to prove that this is possible, I've extended the Timer
| component to include an automatic shutoff after a certain number of
[quoted text clipped - 141 lines]
| Public count As Integer = 1
| End Class