Detecting clicking on the "X" closedown

garcon

Well-known member
Joined
Dec 13, 2004
Messages
47
Programming Experience
Beginner
Folks,

How do you detect the clicking of the little black "X" in the top right-hand corner of a pop-up window of a VB.NET form?

Thanking you,
G.
 

garcon

Well-known member
Joined
Dec 13, 2004
Messages
47
Programming Experience
Beginner
I'm trying:

Private Sub Form_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

FileWrite()

End Sub

but under the closing it says: Event closing cannot be found
 

kanak

Member
Joined
Jan 9, 2005
Messages
17
Programming Experience
Beginner
its "onclosed" event

if u r using vb.net
go to code view

at the top pane there will be two drop down box ....

on the left side drop down box select "overrides" and right side select
"onclosed " and ur subroutine will be generated .. u can write the code here ..and whenever u click the close X button on ur form .. this subroutine will be executed..

hope it will help u
 

toolman

Member
Joined
Sep 7, 2004
Messages
8
Programming Experience
10+
if you typed in this form_closing(...) event yourself rather than select it from the object and event drop downs, you'll get the error you're getting - can't be found..... why? because the form_closing() event hasn't been registered under your region section. open this up and see if the event is being shown there.


toolman.
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
form_closing is the vb6 way, which is NOT supported in .net

there are two close-releated events in vb.net:

Closing which is fired when the form is about to be closed (can can cancel a close in this event by useing e.cancel = (true/false)

and:

Closed which is fired when the form is closed and this is where you put your save settings code

here's an example:
VB.NET:
   Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
  	e.Cancel = True
  End Sub
   
 Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
 	FileWrite()
  End Sub

Also note that these two events fire when the form is closeing IE if you have a button (btnExit) or a menu item (mnuExit) which contains "Me.Close" that will produce the same result as if someone click the "X" in the top corner of the form. Because Me.Close will close the form and those events are fired.
 
Last edited:

garcon

Well-known member
Joined
Dec 13, 2004
Messages
47
Programming Experience
Beginner
JuggaloBrotha,

Sorry man but for the "Form1_Closing" i'm still gettinga : "Event 'Closed' cannot be found" type error message.

I'm totally confused...

Can anyone help?
Please!
G.
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
in the designer Code View in the top left portion of the window (just above where to cade starts) there's two drop down lists, the left one displays all objects IE buttons, menus, textboxes, labels, etc... and once you select one the right hand one displays all the events you can use/code for. so in the left one select the forth from the top titled "(Form1 Events)" yes it's in parenthasis. now on the right select Closed or Closing now the editer adds:

Private Sub Form1.... HANDLES Form1.Closing (or Form1.Closed if that's the event you selected from the right hand drop-down)

End Sub

if your editer does not do that you're either doing it wrong (buy and read several books) or you're not useing vb.net (any version) and you need to upgrade from vb6 (or older) then start posting on this forum. :)
 
Top Bottom