Trapping the form's onclosing event?

G

gsb58

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
 
D

Doug Bell

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
 
G

gsb58

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
 
G

gsb58

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
 

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