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

Trapping the form's onclosing event?

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
5 2106
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
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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Wugi | last post by:
I'm trying to find an equivalent of key-event trapping which is easy in QBasic. Several of my programs build up a geometric figure with two parameter line families, eg with two nested for..next...
1
by: Jules Winfield | last post by:
My WinForms application can have any number of top level forms open at a given time. If the user selects File|Exit, all of the forms are closed. The loop to close the forms looks something like this:...
3
by: Paul | last post by:
I have an Access 2000 database with a form that is giving me some major headaches. When you open the form, it displays all records and allows editing, but has AllowAdditions set to False so that...
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
8
by: Marty | last post by:
Hi, How can I detect that a VS.NET2003 C# form is closed with the upper right "X" button ? This is because I want to add code to the myForm.OnClosing() event to do a different process if a...
2
by: Steve McLellan | last post by:
Hi all, I'm trying to override Form events (for example OnClosing or OnClose) with the intention of preventing the base class function from happening. My functions are getting called but so are...
1
by: Phill W. | last post by:
Has any come across a situation where, in a Form-derived .. er .. Form, the Event Arguments passed to OnClosing /already/ have their Cancel argument set to True? Protected Overrides Sub...
2
by: polocar | last post by:
Hi, I'm writing a C# program that interfaces with a SQL Server 2005 database (I use Visual Studio 2005 Professional Edition as compiler). I have defined the classical ADO.NET SqlDataAdapter,...
2
by: polocar | last post by:
Hi, suppose that you have a C# form with two buttons, that are the classical "btnOk" and "btnCancel" (besides them, of course in the form there can be many other controls). When the user clicks...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.