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

Hello

I am using VB.Net (VS 2003) to develop a multiform app in the compact framework for a Pocket PC. I am having fits trying to prevent multiple instances of forms from opening.

Here is my code with some comments. Please help fix this so no more than one instance of a form can be open.

Thanks

PB

'supposed to check to see if a form is already open but it just keeps opening multiple instances. I can only have one.


    Private Sub butCurBlk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butCurBlk.Click
        Dim Form2 As Form2
        If Not IsNothing(Form2) Then
            Form2.Show()
            Form2.BringToFront()
        Else
            Form2 = New Form2
            Form2.Show()
            Form2.BringToFront()
        End If
    End Sub
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.

william007Commented:

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
Alexandre SimõesManager / Solutions ArchitectCommented:
To make your code work just change it to:

    Dim Form2 As Form2
    Private Sub butCurBlk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butCurBlk.Click
        If Not IsNothing(Form2) Then
            Form2.Show()
            Form2.BringToFront()
        Else
            Form2 = New Form2
            Form2.Show()
            Form2.BringToFront()
        End If
    End Sub

the Form2 Declaration must be declared outside the button click event. Otherwise it will allways be nothing.

Alex :p
william007Commented:
AlexCode, this will work, but have a problem potentially....
try to close the form2 and click on the button, you may see the error...
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Alexandre SimõesManager / Solutions ArchitectCommented:
Can't see the error...
The only thing it's "less well" on that code is regarding the IsNothing (VB6 way of doing it).

    Dim Form2 As Form2
    Private Sub butCurBlk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butCurBlk.Click
        If Not Form2 Is Nothing Then
            Form2.Show()
            Form2.BringToFront()
        Else
            Form2 = New Form2
            Form2.Show()
            Form2.BringToFront()
        End If
    End Sub


This is the easy way of doing this...

Alex :p
william007Commented:
To reproduce the error,

first create two form Form1 and Form2
type the following code in form1
Dim Form2 As Form2
    Private Sub butCurBlk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butCurBlk.Click
        If Not IsNothing(Form2) Then
            Form2.Show()
            Form2.BringToFront()
        Else
            Form2 = New Form2
            Form2.Show()
            Form2.BringToFront()
        End If
    End Sub

then click the button once, the form 2 should appear.
Then close the form2,
click the button again..you should get the
'System.ObjectDisposedException' error..I have tested it on my machine, and I am using VB.Net (VS 2003).
Alexandre SimõesManager / Solutions ArchitectCommented:
Ho... damn... I forgot all about object disposal... sorry...

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

Now it works...

Alex :p
william007Commented:
Glad that you notice this:)
Alexandre SimõesManager / Solutions ArchitectCommented:
:( completly forgot the disposal issue...

Ho well...

Alex :p
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
.NET Programming

From novice to tech pro — start learning today.