468,249 Members | 1,487 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,249 developers. It's quick & easy.

open form from another form.

I know how to open a form from another form. Say open form2 from form1
using a command button. But my problem is everytime I clicked the
button it open again another instance of that form.

here's my code

Dim Course As New Course
Course.Show()

My question is on how to prevent a form from opening again if it is
already open.

Thanks in advance.
Nov 21 '05 #1
4 9547
Hi,

you could just disable the button if the form is opened and then enable the
button again if the form is closed.
If your using a mdi application you could use this code in the mdiParent,
for example in the click event of a menu. You just pass the form name to the
IsOpen function and it checks if that form is already open, if it is it
brings it to the front, else it opens a new instance

hth Greetz Peter

Private blnActive as Boolean

'in Click event
If blnActive = IsOpen("frmGebruikersGrid") Then
Dim objGebruiker As New frmGebruikersGrid(Me)
objGebruiker.WindowState = FormWindowState.Maximized
objGebruiker.Show()
End If
Private Function IsOpen(ByVal nameForm As String) As Boolean
Dim childfrm As Form
For Each childfrm In Me.MdiChildren
strNaam = childfrm.GetType.ToString
intPuntje = strNaam.LastIndexOf(".")
strNaam = Mid(strNaam, intPuntje + 2, Len(strNaam) - intPuntje)
If LCase(strNaam) = LCase(nameForm) Then
childfrm.BringToFront()
Return True
End If
Next
Return False
End Function

"jaYPee" <hi******@yahoo.com> wrote in message
news:nr********************************@4ax.com...
I know how to open a form from another form. Say open form2 from form1
using a command button. But my problem is everytime I clicked the
button it open again another instance of that form.

here's my code

Dim Course As New Course
Course.Show()

My question is on how to prevent a form from opening again if it is
already open.

Thanks in advance.

Nov 21 '05 #2
"jaYPee" <hi******@yahoo.com> schrieb:
My question is on how to prevent a form from opening again if it is
already open.


In addition to the other solutions: You can implement the Singleton design
pattern for your forms in order to access them by their class name:

Implementing the Singleton Pattern in C#
<URL:http://www.yoda.arachsys.com/csharp/singleton.html>

Exploring the Singleton Design Pattern
<URL:http://msdn.microsoft.com/library/en-us/dnbda/html/singletondespatt.asp>

Design Pattern: Singleton in C#
<URL:http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=486>

Design Patterns: Singleton
<URL:http://www.dofactory.com/Patterns/PatternSingleton.aspx>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3
Unless you are smarter than me, doing the thing in P.P first statement is not
that easy. I learned to do a similar thing from a response that I received
from HKW. Question was submitted on 10-16-04. To see full question search
that date for my screen name. His response is shown below. The reference
provided by HKW did the trick.

My resulting code looked like the following:

Private Sub priOpenPreviewForm()
Dim frmPreview As New frmPreview
AddHandler frmPreview.Closed, AddressOf frmPreviewClosed
frmPreview.Show()
Debug.WriteLine("This code executes immediately")
End Sub

Private Sub frmPreviewClosed(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim sMsg As String = "The instance of frmPreview just closed." & vbCrLf
sMsg &= "You may want to use the Closing event if you need to query the form"
sMsg &= " for any changed properties."
Debug.WriteLine(sMsg)
End Sub
=========================
"genojoe" <ge*****@discussions.microsoft.com> schrieb:
My thinking is that I need to create an Event in parent that
fires when MyChild closes.


You need to add a handler to the form's 'Closed' event to get notified when
the form closes:

<URL:http://groups.google.de/groups?selm=2ocnd6F8vepdU2%40uni-berlin.de>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #4
Thanks. Your code works except I have added some code to make it a
child form.

Thanks again.
On Wed, 2 Feb 2005 09:59:18 +0100, "Peter Proost"
<pp*****@nospam.hotmail.com> wrote:
Hi,

you could just disable the button if the form is opened and then enable the
button again if the form is closed.
If your using a mdi application you could use this code in the mdiParent,
for example in the click event of a menu. You just pass the form name to the
IsOpen function and it checks if that form is already open, if it is it
brings it to the front, else it opens a new instance

hth Greetz Peter

Private blnActive as Boolean

'in Click event
If blnActive = IsOpen("frmGebruikersGrid") Then
Dim objGebruiker As New frmGebruikersGrid(Me)
objGebruiker.WindowState = FormWindowState.Maximized
objGebruiker.Show()
End If
Private Function IsOpen(ByVal nameForm As String) As Boolean
Dim childfrm As Form
For Each childfrm In Me.MdiChildren
strNaam = childfrm.GetType.ToString
intPuntje = strNaam.LastIndexOf(".")
strNaam = Mid(strNaam, intPuntje + 2, Len(strNaam) - intPuntje)
If LCase(strNaam) = LCase(nameForm) Then
childfrm.BringToFront()
Return True
End If
Next
Return False
End Function

"jaYPee" <hi******@yahoo.com> wrote in message
news:nr********************************@4ax.com.. .
I know how to open a form from another form. Say open form2 from form1
using a command button. But my problem is everytime I clicked the
button it open again another instance of that form.

here's my code

Dim Course As New Course
Course.Show()

My question is on how to prevent a form from opening again if it is
already open.

Thanks in advance.


Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by BD | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.