REPEAT QUESTION: How to prevent multiple form instances in VB.net (compact framework for the Pocket PC)

Hello

This is another attempt at solving an issue with respect to multiple instances of a form.

Here is what I want to do:

Button_1: When clicked it will check to see if an instance of the form exists. If it does it will get rid of it and open a new blank one.

Button_2: When clicked it will check to see if an instance of the form exists. If it does it will revert to it. If it does not it will open a new one. The following code was offered previously but there is a problem:

        If (Not IsNothing(Form2)) AndAlso (Not Form2.IsDisposed) Then
            Form2.Show()
            Form2.BringToFront()
        Else
            Form2 = New Form2
            Form2.Show()
            Form2.BringToFront()
        End If

The problem is that 'IsDisposed' is not a member of the Form. Show how do I implement Button_1 and how do I fix Button_2.
I am using VB.Net is VS 2003.

NOTE: The IsDisposed part of the code was an attempt to prevent 'System.ObjectDisposedException' error. This error occurs when the user cliicks Button_2 once, causing Form 2 to appear. If the user closes Form2 then clicks Button_2 again the error appears.


Thanks

PB
PBLackAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PBLackAuthor Commented:
PS: The Dim Form2 As Form2 statement appears outside of the sub as this was a previous problem.
Joe_GriffithCommented:
The issue you present under button two is to generate what is known as a "Singleton" class.  That is, a class (in this case a form) that will only allow you to instantiate a single object and if you attempt to instantiate a second one will simply return the one that already exists.

There are a number of ways to code singleton classes (If you goole for it you will find many references). Singleton classes for forms was, in fact, the default in VB6.  One way to see how it can be done for VB.NET is to create a small test program in VB6 and then convert it to VB.NET.  The single instance form will be converted and work the exact same way in VB.NET.  You can then examine the code changes to see how Microsoft did it.  Basically it involves adding a few extra lines of code in the constructor of each form.
mastooCommented:
Why not just set your Form2 variable = nothing when it closes, and then do this:

        If Form2 is nothing Then
            Form2 = New Form2
        End If
        Form2.Show()
        Form2.BringToFront()
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

Joe_GriffithCommented:
Here's the code that Microsoft inserts into the constructor of a form to make it a singleton:

  Public Sub New()
    MyBase.New()

    If m_vb6FormDefInstance Is Nothing Then
      If m_InitializingDefInstance Then
        m_vb6FormDefInstance = Me
      Else
        Try
          'For the start-up form, the first instance created is the default instance.
          If System.Reflection.Assembly.GetExecutingAssembly.EntryPoint.DeclaringType Is Me.GetType Then
            m_vb6FormDefInstance = Me
          End If
        Catch
        End Try
      End If
    End If
    'This call is required by the Windows Form Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() call
   
  End Sub

#Region "Upgrade Support "
  Private Shared m_vb6FormDefInstance As Form1
  Private Shared m_InitializingDefInstance As Boolean
  Public Shared Property DefInstance() As Form1
    Get
      If m_vb6FormDefInstance Is Nothing OrElse m_vb6FormDefInstance.IsDisposed Then
        m_InitializingDefInstance = True
        m_vb6FormDefInstance = New Form1()
        m_InitializingDefInstance = False
      End If
      DefInstance = m_vb6FormDefInstance
    End Get
    Set(ByVal Value As Form1)
      m_vb6FormDefInstance = Value
    End Set
  End Property
#End Region

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PBLackAuthor Commented:
Joe:

I used the following previously

Created a new class:

Public Class clsGlobals
#Region "Singleton Forms"
    Public Shared Form1 As Form1
    Public Shared Form2 As Form2
    Public Shared Form3 As Form3
#End Region
End Class

In Form2, for example, I used this code to open Form3:

        If clsGlobals.Form3 Is Nothing Then
            clsGlobals.Form3 = New Form3
        End If
        clsGlobals.Form3.Show()
        clsGlobals.Form3.Focus()

It does work but somehow it is overiding the closing event of my Form3. I have this code and it does not fire:

    Private Sub Form3__Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If txtSave.Text = 0 Then
                e.Cancel = True
        End If
    End Sub

I am a rookie with VB.Net and having a hard time come to terms with its functionality. Any suggestions as to why the closing is being blocked by my class?

Thanks

PB
Joe_GriffithCommented:
How do you know the closing event doesn't fire?
PBLackAuthor Commented:
I am using a Dell Axim X30 as my device. In it there is a Switcher Bar that shows what is running in memory. The Form is still listed there after I close the form, thus I am concluding that the closing event is not firing. It must be something to do with my approach to the Singleton issue casuing this but I do not understand why.

PB
Joe_GriffithCommented:
I doubt it.  Unlike desktop windows, on a mobile device closing a program does not remove it from memory.  This is true not just for programs you write in .NET but for any program (Open the calendar, close it, look at the memory)

Many people have complained about this!

In any case you can check to see if the event is firing by putting a break point in the handler or by temporarily placing something in the event handler that will do something obvious.  I typically just add something like MessageBox.Show("Hello") and see if a message box shows up.
PBLackAuthor Commented:
Hi Joe

I have this in the closing event of Form3:

    Private Sub Form3__Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        MsgBox("Closing",MsgBoxStyle.OKOnly)
    End Sub

There is no MsgBox showing when I close Form3 by clicking the X in the upper right....

I am going to close this quetsion as it's topic has shifted. I am awarding you points because your solution works...I am reposting a new question for this closing problem and hope you will join in.

Thanks

PB
PBLackAuthor Commented:
Joe

Figured it out finally....

The X button on a Pocket PC is not a 'close' but a 'minimize'. You need the 'Ok' button to appear instead. I simply set the Minimze property to false in the Form Properties and tada...

Something so difficult was made quites easy.

Grrrrrr
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.