473,396 Members | 1,892 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,396 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 9837
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, strikethrough. In fact all the privileges I...
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 form showing more detailed information about that...
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 that would work, feel free to share them. I...
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 one form called Computers and another form called...
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 set the RecordSource of an un-opened form...
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 different security settings and I want to customize the...
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 displayed when the form is opened? Eg. I select a...
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 form but from 2 different places and only one...
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 then respective form is open. I want to set a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.