473,325 Members | 2,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

How do I detect a form being opened already

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

Mar 31 '06 #1
9 2154
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/de...posedtopic.asp

I hope this helps,

Cor

"RichG" <Ri***@discussions.microsoft.com> schreef in bericht
news:EE**********************************@microsof t.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

Mar 31 '06 #2
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

"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

Mar 31 '06 #3
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:
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

Mar 31 '06 #4
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/de...-us/cpref/html /frlrfsystemwindowsformscontrolclassisdisposedtopic .asp
I hope this helps,

Cor

"RichG" <Ri***@discussions.microsoft.com> schreef in bericht
news:EE**********************************@microso ft.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


Mar 31 '06 #5
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" <en*****@hrmworld.com> schreef in bericht
news:uL**************@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/de...-us/cpref/html

/frlrfsystemwindowsformscontrolclassisdisposedtopic .asp

I hope this helps,

Cor

"RichG" <Ri***@discussions.microsoft.com> schreef in bericht
news:EE**********************************@micros oft.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



Mar 31 '06 #6
Smile wrote:
May I ask what is "AFAIK"?


Aha! One I can answer!
http://www.acronymfinder.com/af-quer...d&string=exact

Andrew
Mar 31 '06 #7
Dear Cor, thank you very much.
Smile

Cor Ligthert [MVP] ¼¶¼g©ó¤å³¹ ...
AFAIK = As Far As I Know. (easy to test in this case, however writing it, Idid not, because it was not really important for the question.).
"Smile" <en*****@hrmworld.com> schreef in bericht
news:uL**************@TK2MSFTNGP12.phx.gbl...
May I ask what is "AFAIK"?

Smile


Mar 31 '06 #8
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
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

Mar 31 '06 #9
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.

"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

Mar 31 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: joebob | last post by:
I've got two forms, Form1 and Form2. Form1 opens invisibly when the database opens. From Form1 (or from a regular module or class module accessed by Form1), is there a way to detect when Form2...
6
by: Ana | last post by:
Hi! I have problems with the following scenario: My application is developed using C# under .NET. It must run on all Windows versions starting from Windows 98. The user must open different...
2
by: alex | last post by:
Hi, how can I detect if an MDI is already open or not in order to prevent it from being opened again. How can I bring the MDI form to the TOP from the parent window. If I do a Dim...
2
by: Welie | last post by:
I apologize if this is a faq. I searched for about 45 minutes and didn't find a good answer but there are many matching posts. I have a form which is based on a simple query to a linked table....
2
by: bobh | last post by:
Hi All, In AccessXP on the main menu screen I give the user the option to open this form in either Edit or Read-Only mode. When the user chooses read-only which I use the following line ...
3
by: lrlebron | last post by:
I am using the following to detect if a popup window is already open. I only want to open a new window if it does not exist or has been closed. <HTML> <HEAD> <script language="JavaScript"...
12
by: Phil | last post by:
I can check for MdiChildren.Length=0, but which event handler should I put this in to detect when a child window is closed? TIA Phil.
6
by: Steve | last post by:
Hi All I have an on-screen keyboard within a POS program I have written in VB.net 2005, for touch screen computers I have it set to 'always on top' so the user can move the cursor to...
36
by: Don | last post by:
I wrote an app that alerts a user who attempts to open a file that the file is currently in use. It works fine except when the file is opened by Notepad. If a text file is opened, most computers...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.