
How can i block opening a form that is already open
It if I understand your problem correctly you would like to have only a
single instance of a class (form) and that the show method for that class be
called on that single instance. If this is the case what you are looking
for is called the "Singleton Design Pattern" and here is an off the top of
my head implementation in VB.NET.
As it was shortened for clarity the designer generated code was removed so
you will have copy and paste the relevant pieces below into your own form.
Public Class Form1
Inherits System.Windows.Forms.Form
'This is a top of the head example of implementing a Singleton in
VB.NET.
'For details on the Singleton Design Patter see
http://www.castle-cadenza.demon.co.uk/single.htm
'Frank Gorgenyi (Microsoft)
'This will be the reference to the single allowed instance.
Private Shared objInitialInstance As Form1
Public Sub New()
MyBase.New()
If objInitialInstance Is Nothing Then
'Since we have never been created we can set our share variables
safely.
objInitialInstance = Me
Else
'This will cause any 'new' operations on the class beyond the
first to fail.
Throw New System.Exception("Class is a Singleton, use the
.Singleton method to get an instance.")
End If
End Sub
Public Shared ReadOnly Property Singleton() As Form1
Get
If Not (objInitialInstance Is Nothing) Then
'If we have an instance return it.
Return objInitialInstance
Else
'Otherwise create a new one and return it
Return New Form1()
End If
End Get
End Property
Public Shared Sub ShowSingleton()
'Use the property to ensure we have a valid instance and call it's
show method.
Singleton.Show()
End Sub
End Class
Hope this helps.
Frank Gorgenyi
Microsoft
Quote:
> There's a particular way, that i dont like, but sometimes u can use it,
try
> it. Not my favorite, but its an idea
> use formname.DefInstance.Show
> > In VB6 writing:
> > formname.show
> > if the form is already open it get focus, but with .net this is not
> possible
> > with the new code a new form is opened every time
> > Thanks, and sorry for my english