Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / Design Time / April 2004

Tip: Looking for answers? Try searching our database.

IExtenderProvider for Components

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JackRazz - 04 Apr 2004 21:08 GMT
Is it possible to add an extended property to components like I can with controls via
IExtenderProvider?

JackRazz
yEaH rIgHt - 04 Apr 2004 23:44 GMT
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 - 05 Apr 2004 22:24 GMT
Yea Right,
You have been helping me quite a bit lately<grin>.  Thanks for the info.  I tried to
extend components and it didn't work.  I'll look at it again a little more, since I
now know it should work.

Thanks -  JackRazz

| 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
yEaH rIgHt - 06 Apr 2004 18:20 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.