473,796 Members | 2,455 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to prevent multiple instances of 2nd form?

Hello,

my app opens a 2nd form (form2) by clicking a button on the first form
(form1). I do not want to open form2 in modal form, but I only want one
instance of form2 open. So if someone re-clicks the button on form1 there
will not be multiple instances of form2. I created a module level boolean
var in form1 and a property in form1 to control the loading of form2, but I
am having a problem resetting the value of this boolean var. Here is what I
have:

'--in form1 I have this property

Public Property frmLoaded() As Boolean
Get
Return bFrmLoaded
End Get
Set(ByVal Value As Boolean)
bFrmLoaded = Value
End Set
End Property

Sub...
If bFrmLoaded.Equa ls(False) Then
form2.Show
bFrmLoaded = True
End If
....
End Sub

In form2 in the closing event I want to reset the value of bFrmLoaded to
False. But when I set a form var to form1 - it is a new instance of the form
Dim frm As New form1
frm.frmLoaded = False

The original form1 does not receive this. I can't remember how I dealt with
this in the past. I have done this before. Just can't remember what I did.
Or is there a better approach to dealing with keeping only one instance of a
form (which is not in modal form or mdi, etc)?

Any suggestions appreciated on how to reset the form1 bFrmLoaded boolean
var from form2.

Thanks,
Rich
Jan 16 '06 #1
4 3884
Hmm...

Well your strategy is not going to work, because very act of accessing the
variable will create a new instance of the form.

This is what you should do, and it's damn easy :)

Dim frm as New Form1

If frm Is Nothing Then
Frm.show
End If

Regards
Cyril Gupta
Jan 16 '06 #2
Rich,

It is a very well hidden property.

You can use the Isdisposed from the form2.

http://msdn2.microsoft.com/en-us/lib...sdisposed.aspx

If this answer is not enough, than reply, however I assume it is enough,

Cor
Jan 16 '06 #3
OK. I found this code snippet that seems to work for my purposes:

Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button2.Click
' If the instance still exists... (ie. it's Not Nothing)
If Not IsNothing(F2) Then
' and if it hasn't been disposed yet
If Not F2.IsDisposed Then
' then it must already be instantiated - maybe it's
' minimized or hidden behind other forms ?
F2.WindowState = FormWindowState .Normal ' Optional
F2.BringToFront () ' Optional
Else
' else it has already been disposed, so you can
' instantiate a new form and show it
F2 = New Form2
F2.Show()
End If
Else
' else the form = nothing, so you can safely
' instantiate a new form and show it
F2 = New Form2
F2.Show()
End If

End Sub

But if anyone has any other or additional suggestions - please share.

Thanks
"Rich" wrote:
Hello,

my app opens a 2nd form (form2) by clicking a button on the first form
(form1). I do not want to open form2 in modal form, but I only want one
instance of form2 open. So if someone re-clicks the button on form1 there
will not be multiple instances of form2. I created a module level boolean
var in form1 and a property in form1 to control the loading of form2, but I
am having a problem resetting the value of this boolean var. Here is what I
have:

'--in form1 I have this property

Public Property frmLoaded() As Boolean
Get
Return bFrmLoaded
End Get
Set(ByVal Value As Boolean)
bFrmLoaded = Value
End Set
End Property

Sub...
If bFrmLoaded.Equa ls(False) Then
form2.Show
bFrmLoaded = True
End If
...
End Sub

In form2 in the closing event I want to reset the value of bFrmLoaded to
False. But when I set a form var to form1 - it is a new instance of the form
Dim frm As New form1
frm.frmLoaded = False

The original form1 does not receive this. I can't remember how I dealt with
this in the past. I have done this before. Just can't remember what I did.
Or is there a better approach to dealing with keeping only one instance of a
form (which is not in modal form or mdi, etc)?

Any suggestions appreciated on how to reset the form1 bFrmLoaded boolean
var from form2.

Thanks,
Rich

Jan 16 '06 #4
Hello Rich,

I think it is a perfectly valid solution.

Note that it combines what Cor and I suggested :)

If you check for IsDisposed without checking for Is Nothing first, you will
get an error, or will create an instance of the form.

Regards
Cyril Gupta
Jan 16 '06 #5

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

Similar topics

1
2699
by: Job Lot | last post by:
How can I prevent multiple instances of MdiChild form? I have a MdiParent form with a DataGrid showing all the clients in the database. User clicks on a row to open MdiChild form which display details of the client. User can have multiple clients open at the same time, but how can I prevent them from opening multiple instances of the client?
12
3239
by: (Pete Cresswell) | last post by:
I know I can open many instances of a given form, but I've never done it. Now I'm analyzing an application where that seems like just the ticket: Many investment funds, *lots* of data points for each fund, and a desire by the users to see several funds presented side-by-side. Is opening, say, five instances of the same form real-world-doable? -- PeteCresswell
4
12247
by: logicalfeline | last post by:
How do you prevent multiple instances of a program from running? Cat
2
6342
by: Brian Keating EI9FXB | last post by:
Hello there, What is the best way in c# to prevent multiple instances of my application running at same time on one machine? thanks Brian
11
21008
by: Clark Stevens | last post by:
I just finished a WinForms app in VB.NET. I want to allow the user to be able to run multiple instances of the program like you can with Notepad and Wordpad. The way it is now, once I run the executable I can not run another instance as long as the first instance is running. How can I change this behavior? Thanks.
8
1277
by: Poohface | last post by:
ok here is what I'm doing at the moment and its no good: \\ Dim ThisForm as FormWhatever Private Sub SomeKindOfClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SomeKindOfClick.Click If ThisForm Is Nothing Then ThisForm = New FormWhatever End If
2
2485
by: Job Lot | last post by:
How can I prevent multiple instances of a C# windows forms application. VB.NET provides My.Application.StartupNextInstance Event to do this. Is there any C# equivalent as well? Thanks
11
12822
by: Tony K | last post by:
I have a MDI application. On the menu toolstrip child forms are selected from one of the menus. I don't want to play the disable/enable menu item game. I have selected that open forms are added to the "Window" menu item. What I'm looking for is when the user clicks on the menu item to open the form, how can I check to see if it's open already and if so then set the focus (if possible). What happens now is if the menu item is clicked...
5
3313
by: Neil | last post by:
"lyle" <lyle.fairfield@gmail.comwrote in message news:48c3dde7-07bd-48b8-91c3-e157b703f92b@f3g2000hsg.googlegroups.com... Question for you. I'm doing something similar, only, instead of opening the forms all at once, I'm opening them as needed. I have a main form with multiple records; and then I have a pop-up form that the user opens with button. The pop-up form contains one record relating to the current record in the main form (but...
0
9680
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
9528
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
10455
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
10006
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
9052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7547
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6788
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
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2925
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.