Windows Develop Bookmark and Share   
 index > Windows Forms General > How to have one instance of the form? VB.net or VS 2005(VB)
 

How to have one instance of the form? VB.net or VS 2005(VB)

Hi

I have windows application and a form. If I open a form named"DetailForm" from main form named "Main1" by clicking the button, it should open only once. The problem I am having is, it is opening many instance of the form every time you click the button.

I don'twant to use Showdialog().

Could anyone please show me the right code to achive this?

Vaish  Thursday, March 15, 2007 1:06 PM

Please read http://en.wikipedia.org/wiki/Singleton_pattern

What areyou looking for is called Singleton class.

In VB code goes like this:

Public Class ExampleForm
Inherits System.Windows.Forms.Form

Private Shared _instance As ExampleForm

Public Shared Shadows ReadOnly Property Instance() As ExampleForm
Get
If _instance Is Nothing Then _instance = New ExampleForm Else _instance.Activate()
Return _instance
End Get
End Property

Private Sub User_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
_instance =
Nothing
End Sub

End class

Mojca  Thursday, March 15, 2007 1:24 PM

Please read http://en.wikipedia.org/wiki/Singleton_pattern

What areyou looking for is called Singleton class.

In VB code goes like this:

Public Class ExampleForm
Inherits System.Windows.Forms.Form

Private Shared _instance As ExampleForm

Public Shared Shadows ReadOnly Property Instance() As ExampleForm
Get
If _instance Is Nothing Then _instance = New ExampleForm Else _instance.Activate()
Return _instance
End Get
End Property

Private Sub User_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
_instance =
Nothing
End Sub

End class

Mojca  Thursday, March 15, 2007 1:24 PM

Hi Mojca,

I've met the same problem as Vaish but I'm using Microsoft Visual C# 2005 Express Edition.

Is there any way to solve it?

Please kindly advise.

Thanks. Smile

eniger  Monday, August 06, 2007 9:19 AM

You can use google to search for other answers

Custom Search

More Threads

• Postback to ASP gridview control from windows form
• cannot convert from 'string' to 'AccountManagement.IAccount'
• dual core processors
• Barcode scanner integration????
• [treeview ] find node based on wildcard
• Arrows instead of plus/minus sign in treeview
• How to mark UserControl to do that behavior in InitializeComponent
• .Net Active X controls embedded in internet explorer - security considerations
• Ugly Fonts
• Extract Superclass Refactoring on UserControl