Apply code to 'x' button on form

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2009
Posts: 31
Reputation: rabbithaveit is an unknown quantity at this point 
Solved Threads: 0
rabbithaveit's Avatar
rabbithaveit rabbithaveit is offline Offline
Light Poster

Apply code to 'x' button on form

 
0
  #1
Mar 26th, 2009
Id like it so that when you click the 'x' button to close the program it doesnt just end.

How do I apply code to the 'x' button?

All help is appreciated, thanks in advance

-Russell aka Rabbit
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 486
Reputation: abu taher is an unknown quantity at this point 
Solved Threads: 25
abu taher's Avatar
abu taher abu taher is offline Offline
Posting Pro in Training

Re: Apply code to 'x' button on form

 
0
  #2
Mar 26th, 2009
what you mean? "it doesnt just end."
You want it will hide? then write code in form_laod
"me.hide" or "form1.hide"
I like sword. Attack or Defense.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 31
Reputation: rabbithaveit is an unknown quantity at this point 
Solved Threads: 0
rabbithaveit's Avatar
rabbithaveit rabbithaveit is offline Offline
Light Poster

Re: Apply code to 'x' button on form

 
0
  #3
Mar 26th, 2009
Id like a message box prompt to come up to ask the user if he or she is sure about their decision.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 218
Reputation: hkdani is an unknown quantity at this point 
Solved Threads: 24
hkdani's Avatar
hkdani hkdani is offline Offline
Posting Whiz in Training

Re: Apply code to 'x' button on form

 
0
  #4
Mar 26th, 2009
Id like it so that when you click the 'x' button to close the program it doesnt just end.
Use the Form_Unload() event

  1. Private Sub cmdX_Click()
  2. unload Me
  3. End Sub
  4.  
  5. Private Sub Form_Unload(Cancel As Integer)
  6. Dim intReturn As Integer
  7.  
  8. intReturn = MsgBox("Ready to Exit", vbOKCancel, "Exit?")
  9. If intReturn = vbOK Then
  10. Else
  11. Cancel = True
  12. End If
  13. End Sub
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 35
Reputation: koolsid is an unknown quantity at this point 
Solved Threads: 6
koolsid's Avatar
koolsid koolsid is offline Offline
Light Poster

Re: Apply code to 'x' button on form

 
0
  #5
Mar 27th, 2009
Hi rabbithaveit

I would suggest the API way as it gives you more flexibility... See picture attached...

Please place a Command Button on the form so that you can exit.

Paste this code in the general declaration of the form...

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Option Explicit
  2.  
  3. Private Declare Function DeleteMenu Lib "user32" _
  4. (ByVal hMenu As Long, ByVal nPosition As Long, _
  5. ByVal wFlags As Long) As Long
  6. Private Declare Function GetSystemMenu Lib "user32" _
  7. (ByVal hwnd As Long, ByVal bRevert As Long) As Long
  8.  
  9. Private Const MF_BYPOSITION = &H400&
  10.  
  11. Private ReadyToClose As Boolean
  12.  
  13. Private Sub RemoveMenus(frm As Form, remove_restore As Boolean, _
  14. remove_move As Boolean, remove_size As Boolean, remove_minimize As Boolean, _
  15. remove_maximize As Boolean, remove_seperator As Boolean, remove_close As Boolean)
  16.  
  17. Dim hMenu As Long
  18.  
  19. '~~> Get the form's system menu handle.
  20. hMenu = GetSystemMenu(hwnd, False)
  21.  
  22. If remove_close Then DeleteMenu hMenu, 6, MF_BYPOSITION
  23. If remove_seperator Then DeleteMenu hMenu, 5, MF_BYPOSITION
  24. If remove_maximize Then DeleteMenu hMenu, 4, MF_BYPOSITION
  25. If remove_minimize Then DeleteMenu hMenu, 3, MF_BYPOSITION
  26. If remove_size Then DeleteMenu hMenu, 2, MF_BYPOSITION
  27. If remove_move Then DeleteMenu hMenu, 1, MF_BYPOSITION
  28. If remove_restore Then DeleteMenu hMenu, 0, MF_BYPOSITION
  29. End Sub
  30.  
  31. Private Sub cmdClose_Click()
  32. ReadyToClose = True
  33. Unload Me
  34. End Sub
  35.  
  36. Private Sub Form_Load()
  37. '~~> Remove the Close system menu item and the menu separator.
  38. RemoveMenus Me, False, False, False, False, False, True, True
  39. End Sub
  40.  
  41. '~~> Cancel if ReadyToClose is false.
  42. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  43. Cancel = Not ReadyToClose
  44. End Sub
Last edited by koolsid; Mar 27th, 2009 at 5:48 am.
Attached Images
File Type: bmp API Way.bmp (153.5 KB, 4 views)
A good excercise for the Heart is to bend down and help another up...

Please Mark your Thread "Solved", if the query is solved...

==>If a post has helped you then Please Rate it!
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 31
Reputation: rabbithaveit is an unknown quantity at this point 
Solved Threads: 0
rabbithaveit's Avatar
rabbithaveit rabbithaveit is offline Offline
Light Poster

Re: Apply code to 'x' button on form

 
0
  #6
Mar 28th, 2009
Thanks for replying guys.

hkdani thanks again. I will be sure to try that solution

Koolsid I like the concept but it seems a bit complicated for mke at the moment....
thanks for the reply. But I dont want answer per say...I prefer solutions. So I can learn from them. I'll be sure to look up the API and put your coding to use soon. So thank you

Will be back with a post if it works hkdani

Thanks again guys

-Russell aka Rabbit
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 31
Reputation: rabbithaveit is an unknown quantity at this point 
Solved Threads: 0
rabbithaveit's Avatar
rabbithaveit rabbithaveit is offline Offline
Light Poster

Re: Apply code to 'x' button on form

 
0
  #7
Mar 29th, 2009
Nah, unfortunately that didnt work hkdani. But thanks for the post

koolsid I'll do some research on API and use your code in the meanwhile.

Will post back with results

-Russell aka Rabbit
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,259
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 540
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: Apply code to 'x' button on form

 
-7
  #8
Mar 29th, 2009
In the code editor click (i think its the right) combo box at the top and choose where it says terminate with a lightning bolt next to it
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 31
Reputation: rabbithaveit is an unknown quantity at this point 
Solved Threads: 0
rabbithaveit's Avatar
rabbithaveit rabbithaveit is offline Offline
Light Poster

Re: Apply code to 'x' button on form

 
0
  #9
Mar 29th, 2009
@koolsid

Does this code just disable the 'x' button?

Thats the conclusion im drawing from your image there.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 31
Reputation: rabbithaveit is an unknown quantity at this point 
Solved Threads: 0
rabbithaveit's Avatar
rabbithaveit rabbithaveit is offline Offline
Light Poster

Re: Apply code to 'x' button on form

 
0
  #10
Mar 29th, 2009
@jbennet

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. In the code editor click (i think its the right) combo box at the top and choose where it says terminate with a lightning bolt next to it

Can you supply an image to support that?

Thanks in advance

-Russell aka Rabbit
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Visual Basic 4 / 5 / 6
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC