472,353 Members | 1,682 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 9743
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: monika | last post by:
Hi all... I am able to select a student's story and open it in a word doc. the teacher then corrects the story, highlight in colors the comments,...
1
by: edself | last post by:
I have a form which displays a subform datasheet of information. I'd like to be able to quickly click on a particular record and open up another...
4
by: Dalan | last post by:
I presume that using an open recordset method is the preferred method of accomplishing what I'm trying to do. Of course, if there are other options...
2
by: XxLicherxX | last post by:
Hi, Can someone tell me how I can open a form to a specific record from a button on another form. Here is what I want to have happen. I have...
15
by: kokostik | last post by:
I did a few searches, but couldn't come up with a clear answer to my question. So here it is: If I am in an open form (MyForm1), is it possible to...
7
by: Steve_Black | last post by:
Hello, I'm toying with the idea of loading a MenuStrip (VB.Net 2005) dynamically based on who is logged into my system. Every user has...
14
by: keri | last post by:
Hi, Simple version of the question..... How do I use the where clause of the open form command to show an account with a matching ID to be...
5
by: BD | last post by:
I am coding with C# in Visual Studio 2005 for a database application residing on remote MS SQL Server 2005. What I want to do is open the same...
3
by: godhulirbalaka | last post by:
Dear Sir/Madam, I am new vb 6.0 user. I am developing Shop Management Program. I have a main form with buttons and menus. when i click any button...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

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.