473,322 Members | 1,425 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,322 software developers and data experts.

Preventing Form from Closing

i want to prevent a form from closing..

to do this i want to handle the formClosing or FormClosed events.
from here i want to prevent the form from closing.

New instance of same form should not be used.

is there any way to do this?

is it posiible to do the same thing with anycode?

Jun 14 '07 #1
3 1530
On Jun 13, 9:54 pm, sravan_reddy001 <sravanganta2...@gmail.comwrote:
i want to prevent a form from closing..

to do this i want to handle the formClosing or FormClosed events.
from here i want to prevent the form from closing.

New instance of same form should not be used.

is there any way to do this?

is it posiible to do the same thing with anycode?
You mean like using "e.Cancel =True" in the form closing event?

Thanks,

Seth Rowe

Jun 14 '07 #2
On 14 Jun, 02:54, sravan_reddy001 <sravanganta2...@gmail.comwrote:
i want to prevent a form from closing..

to do this i want to handle the formClosing or FormClosed events.
from here i want to prevent the form from closing.

New instance of same form should not be used.

is there any way to do this?

is it posiible to do the same thing with anycode?
On your form load code, loop through all open windows to make sure
another instance isn't already open.
If it is, just switch focus to it and abort opening this window.

For Each openWindow As Form In Application.OpenForms
If (openWindow.GetType() = Me.GetType()) Then
'duplicate instance. Send focus to the already
open window
openWindow.Focus()
'abort opening me
Close()

End If
Next

that's one way. The other way is obviously

dim openWindow as form

instead of the button / event saying:

dim frm as new Form1
frm.show()

you could declare frm as a member variable instead of a method
variable, that way you can tell if the window is already open.

dim frm as new Form1 '<-- declared at top of file

'in code

if (frm IsNot Nothing) then
frm = new Form1()
frm.show()
end if
frm.Focus()

but you have to remember to mark it as Nothing when the window is
closed which isn't always easy if the window can be opened from
multiple places. Probably use a global variable for that scenario.

Phill

Jun 14 '07 #3
On Jun 14, 5:41 am, Phillip Ross Taylor
<Phillip.Ross.Tay...@gmail.comwrote:
On 14 Jun, 02:54, sravan_reddy001 <sravanganta2...@gmail.comwrote:
i want to prevent a form from closing..
to do this i want to handle the formClosing or FormClosed events.
from here i want to prevent the form from closing.
New instance of same form should not be used.
is there any way to do this?
is it posiible to do the same thing with anycode?

On your form load code, loop through all open windows to make sure
another instance isn't already open.
If it is, just switch focus to it and abort opening this window.

For Each openWindow As Form In Application.OpenForms
If (openWindow.GetType() = Me.GetType()) Then
'duplicate instance. Send focus to the already
open window
openWindow.Focus()
'abort opening me
Close()

End If
Next

that's one way. The other way is obviously

dim openWindow as form

instead of the button / event saying:

dim frm as new Form1
frm.show()

you could declare frm as a member variable instead of a method
variable, that way you can tell if the window is already open.

dim frm as new Form1 '<-- declared at top of file

'in code

if (frm IsNot Nothing) then
frm = new Form1()
frm.show()
end if
frm.Focus()

but you have to remember to mark it as Nothing when the window is
closed which isn't always easy if the window can be opened from
multiple places. Probably use a global variable for that scenario.

Phill
dim frm as new Form1 '<-- declared at top of file

'in code

if (frm IsNot Nothing) then
frm = new Form1()
frm.show()
end if
frm.Focus()
FYI This type of approach is generally known as the Singleton Pattern,
so the OP may want to do some research on it and see if it would work
for them.

Thanks,

Seth Rowe

Jun 14 '07 #4

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

Similar topics

7
by: Miktor | last post by:
At the minute I am using the following rather unwieldy function to prevent the console window in Dev C++ from closing before I can see the output from my program: // function to prevent the...
5
by: RAJ | last post by:
hi plz tell me how to know "how window is going to close"... i have to right code for X button of forms... plz telll me thanks bye
1
by: Chris Bruce | last post by:
In my application I need a way to distiguish between the following events: 1. When a user closes an MDI child window. 2. When the user closes the MDI parent window which subsequently closes the...
5
by: Iain | last post by:
I would like to prevent the user from closing MDI child windows. However, I would like them to be able to minimize and maximize them. Setting the Control Box to false removes the minimize and...
1
by: **Developer** | last post by:
When I get a closing event in a MID Child form I don't know if the child form is closing or the main form is closing. Is there a way to tell? Thank
4
by: Sam | last post by:
Hi, Once the user has clicked on the close button of my MDI form I do error checking in the Closing event if errors have been found the form should not be closed. How can I prevent the form to be...
4
by: microb0x | last post by:
I have an application with a master form with a tab control containing nine tabs, each tab contains a subform. I have command buttons on my main form including: Save , Cancel , Close. My...
3
by: Bill Davidson | last post by:
All: I have a problem in which a worker thread in my (.dll) assembly isn't allowing the main (.exe) assembly from terminating. Here's the scenario: 1) App.Exe is launched. 2) App.Exe calls...
0
by: Susan Bricker | last post by:
Hi. I have a form that is open with AllowAdditions and AllowEdits both set to False. I don't want to allow edits on this screen at this point in the "flow". Opening up a second form seems to be...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.