Help:Exit sub is not working in UserControl_Terminate Event
Hi All,
I want to execute the Usercontrol_termiante event based on the input to some warning message thrown on clicking the window's 'X' button lies on the top-right of my Usercontrol. But I can't use exit sub inside UserControl_Terminate event. Any comments please.
thanks in advance
Rgds
Surendran
Re: Help:Exit sub is not working in UserControl_Terminate Event
emm...i don't very understand what you mean...
however, if your problem is cannot exit the program when click on windows 'X' button, then you can add
cancel=-1 at the Sub form_unload()
OR
Code:
Private Sub Form_Unload(Cancel As Integer)
Dim quit_pro As Integer
Cancel = -1
quit_pro = MsgBox("Are you sure want to quit program?", vbQuestion + vbYesNo, "Quit Program")
If quit_pro = 7 Then Exit Sub
If quit_pro = 6 Then End
End If
Re: Help:Exit sub is not working in UserControl_Terminate Event
I did a simple test and Exit Sub worked just fine in UserControl_Terminate. You should never directly call UserControl_Terminate: you do trigger the code in it, but it doesn't really serve you any real purpose. It doesn't cause your control to be unloaded, you should really unload the control (and afaik you can't make a usercontrol unload itself without some hacking/workaround).
So, what are you actually trying to accomplish? Why there is an X button in the usercontrol? (That's what I understood from the description.)
Re: Help:Exit sub is not working in UserControl_Terminate Event
Make an event for when the X button in your user control is pressed. Then in the form unload the UserControl there when that event fires.
Re: Help:Exit sub is not working in UserControl_Terminate Event
sorry for the confusion, Actually I am using Activex Control and I load that control in to a FORM. I want to write a priece of code in that Activex control(Usercontrol) Terminate Event. Code is like
<code>
if msgbox ( "Do you want to Exit",vbyesno) = vbno then
exit sub
end if
</Code>
I meant this exit sub is not working here. Eventhough I clicked the No button the form is getting terminated.
Quote:
Originally Posted by chxxangie
emm...i don't very understand what you mean...
however, if your problem is cannot exit the program when click on windows 'X' button, then you can add
cancel=-1 at the Sub form_unload()
OR
Code:
Private Sub Form_Unload(Cancel As Integer)
Dim quit_pro As Integer
Cancel = -1
quit_pro = MsgBox("Are you sure want to quit program?", vbQuestion + vbYesNo, "Quit Program")
If quit_pro = 7 Then Exit Sub
If quit_pro = 6 Then End
End If
Re: Help:Exit sub is not working in UserControl_Terminate Event
sorry for the confusion, Actually I am using Activex Control and I load that control in to a FORM. I want to write a priece of code in that Activex control(Usercontrol) Terminate Event. Code is like
<code>
if msgbox ( "Do you want to Exit",vbyesno) = vbno then
exit sub
end if
</Code>
I meant this exit sub is not working here. Eventhough I clicked the No button the form is getting terminated.
Quote:
Originally Posted by chxxangie
emm...i don't very understand what you mean...
however, if your problem is cannot exit the program when click on windows 'X' button, then you can add
cancel=-1 at the Sub form_unload()
OR
Code:
Private Sub Form_Unload(Cancel As Integer)
Dim quit_pro As Integer
Cancel = -1
quit_pro = MsgBox("Are you sure want to quit program?", vbQuestion + vbYesNo, "Quit Program")
If quit_pro = 7 Then Exit Sub
If quit_pro = 6 Then End
End If
Re: Help:Exit sub is not working in UserControl_Terminate Event
Exit Sub is working perfectly. It only exists that particular sub, meaning it won't process more of the code you've written that would come after the If condition. When UserControl gets as far as terminating, there is no way you can prevent it from unloading. So that crushes your current logic.
Due to limitations in regular access to forms, you'd have to subclass the form from the usercontrol in a way that it prevents unload message from getting passed on to the form's handling routine unless the msgbox has been answered. You'd also have to check the unloading conditions, because you don't want to show the messagebox if Windows is telling the application to close (ie. when Windows is shutting down). I can't remember the exact WM_ message that is passed, but it contains all the required information.
For subclassing there is SelfSub, it is meant for subclassing the usercontrol itself, but you can use it to subclass the parent form as well. If you need an example on how that is done, take a look into my UniLabel project: it draws directly on the container object, meaning it needs to subclass it to get the information it needs. It uses SelfSub.
For quick tutorial on subclassing, basically you're interrupting and handling window messages, messages that are sent by Windows and the application itself. It is what Windows programming is really all about, you see how the things work under the hood.