open form from another form.

J

jaYPee

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.
 
P

Peter Proost

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
 
H

Herfried K. Wagner [MVP]

jaYPee said:
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>
 
G

Guest

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 said:
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/[email protected]>
 
J

jaYPee

Thanks. Your code works except I have added some code to make it a
child form.

Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top