Home All Groups Group Topic Archive Search About

How do I detect a form being opened already

Author
31 Mar 2006 5:10 AM
RichG
In VB 5 I could have a form named frmTest and open it with
frmTest.Show

Now in VB.net I have to
Dim frm as New frmTest
frm.show

The problem is I only want one instance of frmTest open, not a new one each
time.
Is there a way to prevent this, or test for the form being open already?

Thanks
Rich

Author
31 Mar 2006 7:32 AM
Cor Ligthert [MVP]
Rich,

You can use the IsDisposed for that. AFAIK is it handled as a property that
is not direct in intellicense.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformscontrolclassisdisposedtopic.asp

I hope this helps,

Cor

Show quoteHide quote
"RichG" <Ri***@discussions.microsoft.com> schreef in bericht
news:EE546602-4DAC-4817-88AB-1728256FE39F@microsoft.com...
> In VB 5 I could have a form named frmTest and open it with
> frmTest.Show
>
> Now in VB.net I have to
> Dim frm as New frmTest
> frm.show
>
> The problem is I only want one instance of frmTest open, not a new one
> each
> time.
> Is there a way to prevent this, or test for the form being open already?
>
> Thanks
> Rich
>
Author
31 Mar 2006 8:38 AM
Smile
May I ask what is "AFAIK"?

Smile

Cor Ligthert [MVP] ¼¶¼g©ó¤å³¹ ...
Show quoteHide quote
>Rich,
>
>You can use the IsDisposed for that. AFAIK is it handled as a property that
>is not direct in intellicense.
>
>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html
/frlrfsystemwindowsformscontrolclassisdisposedtopic.asp
>
>I hope this helps,
>
>Cor
>
>"RichG" <Ri***@discussions.microsoft.com> schreef in bericht
>news:EE546602-4DAC-4817-88AB-1728256FE39F@microsoft.com...
>> In VB 5 I could have a form named frmTest and open it with
>> frmTest.Show
>>
>> Now in VB.net I have to
>> Dim frm as New frmTest
>> frm.show
>>
>> The problem is I only want one instance of frmTest open, not a new one
>> each
>> time.
>> Is there a way to prevent this, or test for the form being open already?
>>
>> Thanks
>> Rich
>>
>
>
Author
31 Mar 2006 9:10 AM
Cor Ligthert [MVP]
AFAIK = As Far As I Know.  (easy to test in this case, however writing it, I
did not, because it was not really important for the question.).


Show quoteHide quote
"Smile" <enqu***@hrmworld.com> schreef in bericht
news:uLdTZ$JVGHA.4864@TK2MSFTNGP12.phx.gbl...
> May I ask what is "AFAIK"?
>
> Smile
>
> Cor Ligthert [MVP] ¼¶¼g©ó¤å³¹ ...
>>Rich,
>>
>>You can use the IsDisposed for that. AFAIK is it handled as a property
>>that
>>is not direct in intellicense.
>>
>>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html
> /frlrfsystemwindowsformscontrolclassisdisposedtopic.asp
>>
>>I hope this helps,
>>
>>Cor
>>
>>"RichG" <Ri***@discussions.microsoft.com> schreef in bericht
>>news:EE546602-4DAC-4817-88AB-1728256FE39F@microsoft.com...
>>> In VB 5 I could have a form named frmTest and open it with
>>> frmTest.Show
>>>
>>> Now in VB.net I have to
>>> Dim frm as New frmTest
>>> frm.show
>>>
>>> The problem is I only want one instance of frmTest open, not a new one
>>> each
>>> time.
>>> Is there a way to prevent this, or test for the form being open already?
>>>
>>> Thanks
>>> Rich
>>>
>>
>>
>
>
Author
31 Mar 2006 9:09 AM
Smile
Dear Cor, thank you very much.
Smile

Cor Ligthert [MVP] ¼¶¼g©ó¤å³¹ ...
Show quoteHide quote
>AFAIK = As Far As I Know.  (easy to test in this case, however writing it,
I
>did not, because it was not really important for the question.).
>
>
>"Smile" <enqu***@hrmworld.com> schreef in bericht
>news:uLdTZ$JVGHA.4864@TK2MSFTNGP12.phx.gbl...
>> May I ask what is "AFAIK"?
>>
>> Smile
>>
Author
31 Mar 2006 9:15 AM
Andrew Morton
Smile wrote:
> May I ask what is "AFAIK"?

Aha! One I can answer!
http://www.acronymfinder.com/af-query.asp?Acronym=AFAIK&Find=find&string=exact

Andrew
Author
31 Mar 2006 8:13 AM
M O J O
Hi RichG,

I don't know if this is the preferable solution, but it works...

In my example, Form1 opens Form2, but Form2 can only have one instance....

FORM1....

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
        Dim frm As Form2 = Form2.GetInstance
        frm.Show()
        frm.BringToFront()
    End Sub

FORM2....

Public Class Form2

    Private Shared _instance As Form2

    Public Shared Function GetInstance() As Form2
        If _instance Is Nothing Then
            _instance = New Form2
        End If
        Return _instance
    End Function

    Private Sub Form2_FormClosed(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        _instance = Nothing
    End Sub

End Class


Look out for wrappings.

Hope it was what you're looking for. :o)

Kind regards,
M oj o



Show quoteHide quote
"RichG" wrote:

> In VB 5 I could have a form named frmTest and open it with
> frmTest.Show
>
> Now in VB.net I have to
> Dim frm as New frmTest
> frm.show
>
> The problem is I only want one instance of frmTest open, not a new one each
> time.
> Is there a way to prevent this, or test for the form being open already?
>
> Thanks
> Rich
>
Author
31 Mar 2006 3:52 PM
Jim Wooley
MOJO's response is a classic singleton pattern which is a way of ensuring
only one instance of the class (in this case, the form) is used. The one
change to his sample I would make would be to change the form's constructor
private, thereby requiring consumers to use the GetInstance method.

Jim

Show quoteHide quote
> Hi RichG,
>
> I don't know if this is the preferable solution, but it works...
>
> In my example, Form1 opens Form2, but Form2 can only have one
> instance....
>
> FORM1....
>
> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
> As
> System.EventArgs) Handles Button1.Click
> Dim frm As Form2 = Form2.GetInstance
> frm.Show()
> frm.BringToFront()
> End Sub
> FORM2....
>
> Public Class Form2
>
> Private Shared _instance As Form2
>
> Public Shared Function GetInstance() As Form2
> If _instance Is Nothing Then
> _instance = New Form2
> End If
> Return _instance
> End Function
> Private Sub Form2_FormClosed(ByVal sender As Object, ByVal e As
> System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
> _instance = Nothing
> End Sub
> End Class
Author
31 Mar 2006 8:24 AM
R. MacDonald
Hello, Rich,

I'm not aware of anything that replaces the VB "Forms" collection.
There must be something referencing the form while it is being shown,
because it continues to exist after the variable "frm" is no longer in
scope, but I haven't been able to figure out how to get this reference.

Just in case nobody else can tell us what replaces the old VB "Forms",
here are some of my thoughts:

I had a need like yours with an MDI application that allowed several
types of forms to be open simultaneously, but only one of each type.  I
just iterated through the MDI forms MDIChildren collection and compared
the type of each child form with that of the form I was about to open.
If I found it, I activated it.  If I didn't I opened it.

I suppose that if all of your forms (except one at the top of the
hierarchy) have Owner forms you could iterate (recursively from the top
level form) through the OwnedForms doing something similar.

Alternatively, you could declare a shared boolean variable and test it
in the form constructor.  If the flag is set, throw an error.  If it is
not, set it and continue.  Then you could catch the error in the code
that opens the form.  I.e. in the form have something like:

   Public Class Form1
     Inherits System.Windows.Forms.Form

     Private Shared m_Exists As Boolean = False

     Public Sub New()
         MyBase.New()

         If (m_Exists) Then
             Throw New FormAlreadyOpenException("Form " & _
                Me.Name & " already exists!")
         End If
         m_Exists = True

         'This call is required by the Windows Form Designer.
         InitializeComponent()
         'Add any initialization after the InitializeComponent() call
     End Sub

     'Form overrides dispose to clean up the component list.
     Protected Overloads Overrides _
    Sub Dispose(ByVal disposing As Boolean)
         If disposing Then
             If Not (components Is Nothing) Then
                 components.Dispose()
             End If
             m_Exists = False
         End If
         MyBase.Dispose(disposing)
     End Sub
     . . .
     . . .

and for opening it:

     Private Sub Button1_Click(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) _
                Handles Button1.Click
         Try
             Dim frmNew As New Form1
             frmNew.Text = "Single Instance Form "
             frmNew.Show()
             frmNew = Nothing

         Catch ex As FormAlreadyOpenException
             MsgBox(ex.Message)

         End Try
     End Sub

Alternatively you could put the test/set into the Form Load event and
clear it in the Form close event.  That would allow multiple instances
of the form to be created/exist, but should prevent more than one
instance at a time from being shown.

Or, as still another alternative, if you could derive all the "show
once" forms from a common base form, then you could include code in that
base to add the form to a global "FormsAlreadyOpen" collection and use
that like the old VB Forms collection.

Or, wait for someone wiser to tell us where the Forms collection has
gone...  ;-)

Cheers,
Randy


RichG wrote:

Show quoteHide quote
> In VB 5 I could have a form named frmTest and open it with
> frmTest.Show
>
> Now in VB.net I have to
> Dim frm as New frmTest
> frm.show
>
> The problem is I only want one instance of frmTest open, not a new one each
> time.
> Is there a way to prevent this, or test for the form being open already?
>
> Thanks
> Rich
>
Author
31 Mar 2006 5:15 PM
RichG
Thanks guys for the suggestions.  The painters came in today so I'm in a low
productivity mode for the day.
I hope to get back to this this evening or tomorrow.
Thanks again.

Show quoteHide quote
"RichG" wrote:

> In VB 5 I could have a form named frmTest and open it with
> frmTest.Show
>
> Now in VB.net I have to
> Dim frm as New frmTest
> frm.show
>
> The problem is I only want one instance of frmTest open, not a new one each
> time.
> Is there a way to prevent this, or test for the form being open already?
>
> Thanks
> Rich
>