Use the 'X' button of the form

ProgMan

Well-known member
Joined
Nov 25, 2006
Messages
55
Location
UK
Programming Experience
3-5
Hi, :)

I was wondering if it is possible to reassign the 'X' button of a form (top-right).

For example, lets say I have a button or menu option to exit the application. When a user clicks that option, the application performs a specific routine and exits the aplication. However, I don't see a way to perform that task when the user exits the application using the 'X' button of the form.

Is the any way to associate the 'X' button with a specific method/routine ?

Thanks.
 
Use the FormClosing event
 
ur routine ofcourse is in a method ... handle on clicking "X" to call the routine
for example

i want the user when he click exit button to ask him first as confirmation
so wut we will do here is

Public Sub confirm()
msgbox ("Are You Sure you want toexit?")
End Sub



Public Exitbutton_onclick(ByVal ..... bla bla bla) handle exitbutton.click
' call your method here
confirm
End sub

then go to the "x" button

Private Sub Form1_close (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.closed

' call your method here too
confirm()

End sub
 
The Closing/Closed events are obsolete in the .NET Framework version 2.0; use the FormClosing/FormClosed events instead. If you need to prevent the close you have to use the mentioned FormClosing which can be cancelled.

You must also change your confirm sub into a function and catch the dialog return value for it to be usable.
 
You must also change your confirm sub into a function and catch the dialog return value for it to be usable.

why it is must to make it as function ... i don't need to return any values ..
a Messagebox.show (....) can be used in Subroutine means procedure .. and it can be cancelled too as u mentioned in ur reply ...
 
There is no point in a confirmation that doesn't reflect choice to the cancel property of the formclosing event.
 
it sounds very logically ... so how can i do that ..can i handle it like

if Messagebox cancel button pressed
Me.close = false

how can i handle the event of the cancel button of the messagebox.show

can u write the sample of that code plz.. sorry i'm a newbie
 
Check the return value of the MessageBox/MsgBox call and act accordingly setting the e.cancel property.
VB.NET:
If MessageBox.Show("close form?", "confirm", MessageBoxButtons.OKCancel) = [SIZE=2]Windows.Forms.[/SIZE]DialogResult.Cancel Then e.Cancel = True
 
Back
Top