Preventing Form from Closing

S

sravan_reddy001

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?
 
R

rowe_newsgroups

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
 
P

Phillip Ross Taylor

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
 
R

rowe_newsgroups

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top