Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to tell how my user closed my form? 4

Status
Not open for further replies.

bigfoot

Programmer
May 4, 1999
1,779
0
0
US
In the form closing event. How can I tell if I called a me.close in code or the user pressed the X close in the upper right hand corner.
 
Not sure if there is a way. You could add a property to your form say DIM bClose as Boolean = False.

Then instead of calling me.close, call a new method called Me.zclose. Its worth having a custom close method to call through code, to perform any other cleanup code too.

private sub zclose
Me.bClose=True
Me.close
end sub


Sweep
...if it works dont mess with it
 
If you implement WndProc, you can identify it there. Here is the code to do that.

Public Const SC_CLOSE As Integer = 61536
Public Const WM_SYSCOMMAND As Integer = 274

Protected Overloads Overrides Sub WndProc(ByRef m As Message)

If m.Msg=WM_SYSCOMMAND AndAlso m.WParam.ToInt32=SC_CLOSE Then

MessageBox.Show("X clicked")
End If
MyBase.WndProc(m)

End Sub

-Kris
 
Hey Kris11. Thanks for the reply. And I like SqueakinSweep's way too. I started doing SqueakinSweep's way but yours is much cleaner. (no vars to mess with).

Thanks for the timely reply.

Gary
 
Can I do this Kris?

Protected Overloads Overrides Sub WndProc(ByRef m As Message)

If m.Msg=WM_SYSCOMMAND AndAlso m.WParam.ToInt32=SC_CLOSE Then
MessageBox.Show("X clicked")
m.Msg=0 <--- Stop closing
End If
MyBase.WndProc(m)

End Sub
 
I was away from my desk for few weeks. Sorry for the late reply. Yes you can do that, that will prevent the window from closing.

-Kris
 
Kris,

This was something that I was searching for a while. Since bigfoot didn't bother to give you a star, I am going to.
 
I'll give you a star too. I was on mini vacation, and just read this.

Thanks for all the help.
 
Glad I could help, thanks for the stars.

-Kris
 
Thanks kris11, I was just seraching for about 45 min to an hour for this. Works great. I gave you a star for it.

Thanks!!!!
 
pff three stars for an easy question and a short answer ;-).
I'll make that four stars.

Christiaan Baes
Belgium

What a wonderfull world - Louis armstrong
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top