473,698 Members | 2,754 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Avoid opening a second instance of a form

Hi,

How can I make sure I don't show the same form twice? If the form is already
shown, just focus. I tried using ownedforms, but can't quite make it work...

Thanks in advance!

--
Lasse
Nov 20 '05 #1
6 7348
Hi Lasse,

One way is to have a class which your Forms register with when they open
and close. Then, if there's a request for a Form that is already showing it
can be activated and brought to the front.

I answered a question about switching Forms (Topic: Main Menu, 13th Sep).
There's a class given (and a project attached) that may give you some ideas.
It's about closing and opening Forms, but I think it may be adapted to suit
your needs.

Add another question here if you need further help. :-)

Regards,
Fergus
Nov 20 '05 #2
Hello,

"Lasse Eskildsen" <Le****@DELETEw ebspeed.dk> schrieb:
How can I make sure I don't show the same form twice?
If the form is already shown, just focus. I tried using
ownedforms, but can't quite make it work...


Have a Google Search on the "singleton design pattern".

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #3
Hi Lasse

I use the following function in an MDI application to avoid opening the same
MDI child form twice:

<code>
Private Function IsFormOpen(ByVa l Title As String, ByVal ActivateOnFound As
Boolean) As Boolean

Dim frm As System.Windows. Forms.Form
Dim IsOpen As Boolean = False

For Each frm In Me.MdiChildren
If frm.Text = Title Then
' It is open
If ActivateOnFound Then
frm.Activate
End If

IsOpen = True

Exit For
End If
Next frm

Return IsOpen

End Function
</code>

It assumes that you know the title/caption of the form that you are trying
to open.

Called like this:

<code>
If Not IsOpen("Some Form", True) Then
' Create new instance of "Some Form" and open it
End If
</code>

HTH

Charles
"Lasse Eskildsen" <Le****@DELETEw ebspeed.dk> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Hi,

How can I make sure I don't show the same form twice? If the form is already shown, just focus. I tried using ownedforms, but can't quite make it work...
Thanks in advance!

--
Lasse

Nov 20 '05 #4
Hi Lasse, Charles,

My suggestion is for the non-MDI situation. Essentially you have to
implement the effect of Charles' code, but you also have to maintain your own
version of MdiChildren.

Regards,
Fergus
Nov 20 '05 #5
Hi,
Thanks for the help to everybody. I use both mdi and non-mdi from my
mainform, so with some help from you guys, I wrote the function below:
Private Function FormIsOpen(ByVa l Titel As String, ByVal ActivateOnFound As
Boolean) As Boolean

Dim frm As System.Windows. Forms.Form

Dim IsOpen As Boolean = False

For Each frm In Me.MdiChildren

If frm.Text = Titel Then 'The form is open

If ActivateOnFound Then

frm.Activate()

End If

IsOpen = True

End If

Next

For Each frm In Me.OwnedForms

If frm.Text = Titel Then 'The form is open

If ActivateOnFound Then

frm.Activate()

End If

IsOpen = True

End If

Next

Return IsOpen

End Function

And use me.addownedform () for non-mdi forms.Thanks for the help!

--

Lasse

"Lasse Eskildsen" <Le****@DELETEw ebspeed.dk> skrev i en meddelelse
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Hi,

How can I make sure I don't show the same form twice? If the form is already shown, just focus. I tried using ownedforms, but can't quite make it work...
Thanks in advance!

--
Lasse

Nov 20 '05 #6
"Lasse Eskildsen" <Le****@DELETEw ebspeed.dk> schrieb
How can I make sure I don't show the same form twice? If the form is
already shown, just focus. I tried using ownedforms, but can't quite
make it work...


Store the reference to the Form. Next time, you'll know that it's already
shown by comparing the reference to Nothing. When the Form is closed, set
the reference to Nothing.
--
Armin

Nov 20 '05 #7

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

Similar topics

44
4718
by: Carlos Andr?s | last post by:
Hi everybody. I've got a problem. I'd like to avoid opening a new window when you have pressed the shift key and you click in the left button of the mouse. I've tried the next solution, in the body of the page I put the next code: <BODY onkeydown='notOpenNewWindow();'>
13
2745
by: Steve Jorgensen | last post by:
== On Error Resume next, and Err.Number == If you want to call one of your procedures from another procedure, and check for errors afterward, you mayimagine that you should write code something like this... On Error Resuse Next MyFoo 123 lngErrNum = Err.Number On Error Goto 0
11
1999
by: MLH | last post by:
My alzheimer moments occur with greater frequency these days, it seems. I sometimes open Access and the same dbase in a second instance and work on items. If I modify a form and discover later that I'm working in a 2nd instance of Access, I close it. The modifications are not present in the first instance of the program and I have to make them again. Is there a setting I can make that will prevent me from inadvertently doing just that?
11
12792
by: Ahmet AKGUN | last post by:
Hi; is it possible to open one form in .net platform that we have its name in string ? I have string sFormName = "frmCustomer"; and I must automatically open Customer form. or is it possible to get one referance of an item on form ?
6
2000
by: bole2cant | last post by:
The error I get is: Cannot access a disposed Object named "Form2". Object name "Form2". Here is the sequence which gives the error. Start program. Form1 comes up and has a button to choose Form2. Go to Form2. Close Form2 when done. Now back on Form1. Click button to go back to Form2. The error/exception comes up. I originally got this error when using Me.Close(), so I changed it to Me.Hide() and everything was fine. Except now when...
10
1627
by: Norm | last post by:
I must not be understanding something about the use of forms in VB.Net that is different from VB. I have one form running in the background with an icon in the task bar. Right clicking on the icon gives you a list of options, one of which is to show an About form. After this form shows I get an error saying "Object reference not set to an instance of the object." I have tried opening the form with frmAbout.Show frmAbout.ShowDialog
16
5180
by: Phil Stanton | last post by:
I have a form with a button which is supposed to open an Excel file (With lots of Macros /VBA) in it. The Excel file gets it's data from the Access program Here is the code Private Sub Storage_Click() On Error GoTo Err_Storage_Click
2
4229
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
I am (still) relatively new to Windows applications, most of my experience has been Web based, and I am confused about what exactly happens when the Main() method is called and how to manipulate forms opening & closing. An example of this issue is as follows. I have a logon form open as the first thing. The main functional form opens when a user has successfully logged on. From the main form, a user should be able to logout which will...
6
2168
by: lyle fairfield | last post by:
"dpcman01" <bob@dpcman.com.auwrote in news:48e4a87b$0$31804$afc38c87@news.optusnet.com.au: Much of the code at the MVP site gave excellent results last century. If I were opening a report in another db I would (using Northwind 2007.accdb as an example) 1. Write a procedure in that db as:
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.