Preventing Multiple Instances of a Component

J

JackRazz

I'm writing a component (NOT A CONTROL) that need's to determine if an instance of
the component is already placed on a form and, if so, kill the new instance.
Anotherwords I only want one instance of a component on a form.

Does any have any ideas on how I could do this? If its possible to walk the
component list, does anyone have any sample code?

Thanks - JackRazz
 
A

Anand Balasubramanian

Hi,
I looked at this issue.. There may be no straight forward way to do this
at desgin time. But one alternative I can think of is, you can create a
class library and then basically create a singleton design pattern such
that only one instance of your class exists in the program. If you want to
take singleton pattern approach, then do let me know, i can send a sample
to you.
Thanks
Anand Balasubramanian
Microsoft, Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks
 
Y

yEah rIgHt

Try this: Create a new class as shown below. Then add an designer
attribute to your component class (see below the MyComponentDesigner class).

Public Class MyComponentDesigner
Inherits ComponentDesigner

Private CCService As IComponentChangeService

Public Overrides Sub Initialize(ByVal component As
System.ComponentModel.IComponent)
MyBase.Initialize(component)

CCService =
DirectCast(MyBase.GetService(GetType(IComponentChangeService)),
IComponentChangeService)


AddHandler CCService.ComponentAdding, AddressOf
MyComponentDesigner_ComponentAdding
End Sub



Private Sub MyComponentDesigner_ComponentAdding(ByVal sender As
Object, ByVal e As ComponentEventArgs)
'code here
Throw New Exception("Only one instance of this component is
allowed")
End Sub



Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

RemoveHandler CCService.ComponentAdding, AddressOf
MyComponentDesigner_ComponentAdding
MyBase.Dispose(disposing)
End Sub

End Class



Then put this in your component class:

<Designer(GetType(MyComponentDesigner))> _
Public Class YourComponent
Inherits System.ComponentModel.Component

'-- Your code here


End Class
 
J

JackRazz

Very interesting, The component services is an area I don't know a thing about. I'm
going to look at it and see if I can get it to work.

I appreciate this code snippet - JackRazz


| Try this: Create a new class as shown below. Then add an designer
| attribute to your component class (see below the MyComponentDesigner class).
|
| Public Class MyComponentDesigner
| Inherits ComponentDesigner
|
| Private CCService As IComponentChangeService
|
| Public Overrides Sub Initialize(ByVal component As
| System.ComponentModel.IComponent)
| MyBase.Initialize(component)
|
| CCService =
| DirectCast(MyBase.GetService(GetType(IComponentChangeService)),
| IComponentChangeService)
|
|
| AddHandler CCService.ComponentAdding, AddressOf
| MyComponentDesigner_ComponentAdding
| End Sub
|
|
|
| Private Sub MyComponentDesigner_ComponentAdding(ByVal sender As
| Object, ByVal e As ComponentEventArgs)
| 'code here
| Throw New Exception("Only one instance of this component is
| allowed")
| End Sub
|
|
|
| Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
|
| RemoveHandler CCService.ComponentAdding, AddressOf
| MyComponentDesigner_ComponentAdding
| MyBase.Dispose(disposing)
| End Sub
|
| End Class
|
|
|
| Then put this in your component class:
|
| <Designer(GetType(MyComponentDesigner))> _
| Public Class YourComponent
| Inherits System.ComponentModel.Component
|
| '-- Your code here
|
|
| End Class
|
|
|
| > Hi,
| > I looked at this issue.. There may be no straight forward way to do this
| > at desgin time. But one alternative I can think of is, you can create a
| > class library and then basically create a singleton design pattern such
| > that only one instance of your class exists in the program. If you want to
| > take singleton pattern approach, then do let me know, i can send a sample
| > to you.
| > Thanks
| > Anand Balasubramanian
| > Microsoft, Visual Basic .NET
| >
| > This posting is provided "AS IS" with no warranties, and confers no rights.
| > Please reply to newsgroups only. Thanks
| >
 
Y

yEah rIgHt

JackRazz, I forgot to add one thing. Put this into the Adding Event, or
else the user won't be able to add other controls.

Private Sub MyComponentDesigner_ComponentAdding(ByVal sender As
Object, ByVal e As ComponentEventArgs)

'-- Use your class name here.
If TypeOf e.Component Is YourClassNameHere Then
Throw New Exception("Only one instance of this component is
allowed.")
End If

End Sub


The designer only works at design time so a user may try to add another
control at run time. To prevent this, you need to add a Shared variable
to your class so you can error out if they try to add another instance.
See example:

Class YourClass

Shared inUse as Boolean = False


Public Sub New()
MyBase.New()

'-- If inUse then no more of this componet may be added
If inUse Then
Throw (New Exception("Only one instance of this componet
may be run."))
End If

'-- Set inUse to True
inUse = True

'This call is required by the Component Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'--- Make sure you set inuse to Flase when disposing
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then

'-- No longer in use, set to false
inUse = False
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub




End class
 
J

JackRazz

I have to figure out how all this works (what a learning project for the weekend!).
Also, just have to say I get a kick out of your handle and email address. I use
(e-mail address removed) and I thought mine was good<grin>.

Thanks - Jack


| JackRazz, I forgot to add one thing. Put this into the Adding Event, or
| else the user won't be able to add other controls.
|
| Private Sub MyComponentDesigner_ComponentAdding(ByVal sender As
| Object, ByVal e As ComponentEventArgs)
|
| '-- Use your class name here.
| If TypeOf e.Component Is YourClassNameHere Then
| Throw New Exception("Only one instance of this component is
| allowed.")
| End If
|
| End Sub
|
|
| The designer only works at design time so a user may try to add another
| control at run time. To prevent this, you need to add a Shared variable
| to your class so you can error out if they try to add another instance.
| See example:
|
| Class YourClass
|
| Shared inUse as Boolean = False
|
|
| Public Sub New()
| MyBase.New()
|
| '-- If inUse then no more of this componet may be added
| If inUse Then
| Throw (New Exception("Only one instance of this componet
| may be run."))
| End If
|
| '-- Set inUse to True
| inUse = True
|
| 'This call is required by the Component Designer.
| InitializeComponent()
|
| 'Add any initialization after the InitializeComponent() call
|
| End Sub
|
| '--- Make sure you set inuse to Flase when disposing
| Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
| If disposing Then
|
| '-- No longer in use, set to false
| inUse = False
| If Not (components Is Nothing) Then
| components.Dispose()
| End If
| End If
| MyBase.Dispose(disposing)
| End Sub
|
|
|
|
| End class
|
|
|
| > Very interesting, The component services is an area I don't know a thing about.
I'm
| > going to look at it and see if I can get it to work.
| >
| > I appreciate this code snippet - JackRazz
| >
| >
| > | > | Try this: Create a new class as shown below. Then add an designer
| > | attribute to your component class (see below the MyComponentDesigner class).
| > |
| > | Public Class MyComponentDesigner
| > | Inherits ComponentDesigner
| > |
| > | Private CCService As IComponentChangeService
| > |
| > | Public Overrides Sub Initialize(ByVal component As
| > | System.ComponentModel.IComponent)
| > | MyBase.Initialize(component)
| > |
| > | CCService =
| > | DirectCast(MyBase.GetService(GetType(IComponentChangeService)),
| > | IComponentChangeService)
| > |
| > |
| > | AddHandler CCService.ComponentAdding, AddressOf
| > | MyComponentDesigner_ComponentAdding
| > | End Sub
| > |
| > |
| > |
| > | Private Sub MyComponentDesigner_ComponentAdding(ByVal sender As
| > | Object, ByVal e As ComponentEventArgs)
| > | 'code here
| > | Throw New Exception("Only one instance of this component is
| > | allowed")
| > | End Sub
| > |
| > |
| > |
| > | Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
| > |
| > | RemoveHandler CCService.ComponentAdding, AddressOf
| > | MyComponentDesigner_ComponentAdding
| > | MyBase.Dispose(disposing)
| > | End Sub
| > |
| > | End Class
| > |
| > |
| > |
| > | Then put this in your component class:
| > |
| > | <Designer(GetType(MyComponentDesigner))> _
| > | Public Class YourComponent
| > | Inherits System.ComponentModel.Component
| > |
| > | '-- Your code here
| > |
| > |
| > | End Class
| > |
| > |
| > |
| > | > Hi,
| > | > I looked at this issue.. There may be no straight forward way to do this
| > | > at desgin time. But one alternative I can think of is, you can create a
| > | > class library and then basically create a singleton design pattern such
| > | > that only one instance of your class exists in the program. If you want to
| > | > take singleton pattern approach, then do let me know, i can send a sample
| > | > to you.
| > | > Thanks
| > | > Anand Balasubramanian
| > | > Microsoft, Visual Basic .NET
| > | >
| > | > This posting is provided "AS IS" with no warranties, and confers no rights.
| > | > Please reply to newsgroups only. Thanks
| > | >
| >
| >
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top