By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,435 Members | 2,939 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,435 IT Pros & Developers. It's quick & easy.

Trapping the form's onclosing event?

P: n/a
Hi!

In my application I have code that will give the user a choice when he
click's the close button on the ToolBar1.

However, when he clicks the close button of the form, the form will
shut down even if he chooses the cancel button of the messagebox that
pop's up.

Now...if he, on the other hand, chooses ALT+F4, then I catch this in
code and then the cancel button works very well. The code goes like:

Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

Try
If (e.Alt) = Keys.F4 Then
MyShuttingDownAction()
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "HEY!", MessageBoxButtons.OK,
MessageBoxIcon.Warning)
End Try
End Sub

So I've tried to catch the form's close button in the form's OnClosing
event, but it doesn't respond to the code I've written, which goes like
this:

Public Sub MyShuttingDownAction()
Dim intRes As Integer

Try
intRes = MessageBox.Show("U LEAVE ME?", _
"BYE?", _
MessageBoxButtons.OKCancel, MessageBoxIcon.Question)

If intRes = DialogResult.Cancel Then
StatusBar1.Panels(4).Text = "STILL HERE?"
ElseIf intRes = DialogResult.OK Then
Application.Exit()
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "HEY!", MessageBoxButtons.OK,
MessageBoxIcon.Warning)
End Try
End Sub

Hope anybody could point me in the right direction.

Me.Name

Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi,

Dim a boolean that has scope for the form class.

when you meet conditions acceptable for the form to close, set the variable
true.

In the Forms Closing Event, cancel the close if the variable (flag) is not
set.

eg

Private Sub FormLocalSettings_Closing(ByVal sender As Object, _

ByVal e As System.ComponentModel.CancelEventArgs) _

Handles MyBase.Closing

e.Cancel = Not fClose

End Sub

<gs***@hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hi!

In my application I have code that will give the user a choice when he
click's the close button on the ToolBar1.

However, when he clicks the close button of the form, the form will
shut down even if he chooses the cancel button of the messagebox that
pop's up.

Now...if he, on the other hand, chooses ALT+F4, then I catch this in
code and then the cancel button works very well. The code goes like:

Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

Try
If (e.Alt) = Keys.F4 Then
MyShuttingDownAction()
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "HEY!", MessageBoxButtons.OK,
MessageBoxIcon.Warning)
End Try
End Sub

So I've tried to catch the form's close button in the form's OnClosing
event, but it doesn't respond to the code I've written, which goes like
this:

Public Sub MyShuttingDownAction()
Dim intRes As Integer

Try
intRes = MessageBox.Show("U LEAVE ME?", _
"BYE?", _
MessageBoxButtons.OKCancel, MessageBoxIcon.Question)

If intRes = DialogResult.Cancel Then
StatusBar1.Panels(4).Text = "STILL HERE?"
ElseIf intRes = DialogResult.OK Then
Application.Exit()
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "HEY!", MessageBoxButtons.OK,
MessageBoxIcon.Warning)
End Try
End Sub

Hope anybody could point me in the right direction.

Me.Name

Nov 21 '05 #2

P: n/a
Thanks Doug!

It really stopped the form from closing.

However, if I'd like to call MyShuttingDownAction(), how would I go
about?

Or am I way out on the hillside?

Me.Name

Nov 21 '05 #3

P: n/a
Sorry this was a response to the message below in the list

"Doug Bell" <Po*********@vodaphone.com.au> wrote in message
news:e3**************@TK2MSFTNGP12.phx.gbl...
Hi,

Dim a boolean that has scope for the form class.

when you meet conditions acceptable for the form to close, set the variable true.

In the Forms Closing Event, cancel the close if the variable (flag) is not
set.

eg

Private Sub FormLocalSettings_Closing(ByVal sender As Object, _

ByVal e As System.ComponentModel.CancelEventArgs) _

Handles MyBase.Closing

e.Cancel = Not fClose

End Sub

<gs***@hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hi!

In my application I have code that will give the user a choice when he
click's the close button on the ToolBar1.

However, when he clicks the close button of the form, the form will
shut down even if he chooses the cancel button of the messagebox that
pop's up.

Now...if he, on the other hand, chooses ALT+F4, then I catch this in
code and then the cancel button works very well. The code goes like:

Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

Try
If (e.Alt) = Keys.F4 Then
MyShuttingDownAction()
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "HEY!", MessageBoxButtons.OK,
MessageBoxIcon.Warning)
End Try
End Sub

So I've tried to catch the form's close button in the form's OnClosing
event, but it doesn't respond to the code I've written, which goes like
this:

Public Sub MyShuttingDownAction()
Dim intRes As Integer

Try
intRes = MessageBox.Show("U LEAVE ME?", _
"BYE?", _
MessageBoxButtons.OKCancel, MessageBoxIcon.Question)

If intRes = DialogResult.Cancel Then
StatusBar1.Panels(4).Text = "STILL HERE?"
ElseIf intRes = DialogResult.OK Then
Application.Exit()
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "HEY!", MessageBoxButtons.OK,
MessageBoxIcon.Warning)
End Try
End Sub

Hope anybody could point me in the right direction.

Me.Name


Nov 21 '05 #4

P: n/a
Hi,

Have a look at the closing event of the form and in that the e.cancel.

http://msdn.microsoft.com/library/de...osingTopic.asp

I hope this helps

Cor
Nov 21 '05 #5

P: n/a

Hi!

I solved it

I just added the private sub MyShuttingDownAction:

Dim intRes As Integer
Try
intRes = MessageBox.Show("U LEAVE ME?", _
"BYE?", _
MessageBoxButtons.OKCancel, MessageBoxIcon.Question)

If intRes = DialogResult.Cancel Then
StatusBar1.Panels(4).Text = "CANCELLED"
e.Cancel = True
ElseIf intRes = DialogResult.OK Then
e.Cancel = False
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "HALLÅ!",
MessageBoxButtons.OK, MessageBoxIcon.Warning)
End Try

Thanks anyway!

Me.Name

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.