|
-
Aug 2nd, 2007, 04:34 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] exit
how can I make the programme exit itself If I press a button, simular to if somebody pressed the red cross in the corner?
thankyou.
-
Aug 2nd, 2007, 05:19 PM
#2
Re: exit
Thread moved from CodeBank forum (which is for code examples, not questions). As your other posts are in Classic VB, I assumed this is where you want it
If you are actually using Classic VB, simply use this:
-
Aug 2nd, 2007, 05:22 PM
#3
Lively Member
Re: exit
Yeah thats they way but with everything it will look like
vb Code:
Private Sub Command1_Click()
Unload Me
End Sub
Since i can tell you are new you may of not known that.
-
Aug 2nd, 2007, 08:46 PM
#4
Re: exit
Unload Me is the proper answer if you have only one form loaded. If there's any chance that there's more than one then do this
Code:
Dim frm As Form
For Each frm In Forms
Unload frm
set frm = Nothing
Next
-
Aug 3rd, 2007, 01:30 AM
#5
Re: exit
Just a note on martin's code you shouldn't really put it in the unload event of the main form for the reason being. It will try to unload the form even though it is already in the process of unloading.
-
Aug 3rd, 2007, 01:39 AM
#6
Frenzied Member
Re: exit
Hey cant we Use End or End Sys ???
Like
Code:
Private Sub Command1_Click()
End
End Sub
Since Kaimonington says that exit the programme, not close a form ????
-
Aug 3rd, 2007, 07:39 AM
#7
Re: exit
 Originally Posted by Hell-Lord
Just a note on martin's code you shouldn't really put it in the unload event of the main form for the reason being. It will try to unload the form even though it is already in the process of unloading.
There's no problem putting the code in the Unload event.
-
Aug 3rd, 2007, 01:41 AM
#8
Re: exit
End is not an appropriate method of closing a project.
The reason being it doesn't allow the program to clean up after it self. Memory issue can also occur.
Last edited by Paul M; Aug 3rd, 2007 at 01:45 AM.
-
Aug 3rd, 2007, 02:07 AM
#9
Hyperactive Member
Re: exit
Hiya Martin
If I have already unloaded a form then why do I have to set it equal to nothing and if it has already been unloaded why does assigning it to anything not generate an error? I do it only because I have been told I have to but it makes me cringe every time.
-
Aug 3rd, 2007, 02:25 AM
#10
Frenzied Member
Re: exit
yes Hell-Lord is right. This is what is said about End in MSDN
Remarks
When executed, the End statement resets all module-level variables and all static local variables in all modules. To preserve the value of these variables, use the Stop statement instead. You can then resume execution while preserving the value of those variables.
Note The End statement stops code execution abruptly, without invoking the Unload, QueryUnload, or Terminate event, or any other Visual Basic code. Code you have placed in the Unload, QueryUnload, and Terminate events of forms and class modules is not executed. Objects created from class modules are destroyed, files opened using the Open statement are closed, and memory used by your program is freed. Object references held by other programs are invalidated.
The End statement provides a way to force your program to halt. For normal termination of a Visual Basic program, you should unload all forms. Your program closes as soon as there are no other programs holding references to objects created from your public class modules and no code executing
so dont use it to exit the program
-
Aug 3rd, 2007, 02:19 AM
#11
Re: exit
release the handle of the window
-
Aug 3rd, 2007, 02:23 AM
#12
Re: exit
Just to note should come after the otherwise the unload frm line will fail.
-
Aug 3rd, 2007, 03:00 AM
#13
Re: exit
So basically the complete run down is....
vb Code:
Dim frm as Form
For Each frm In Forms
Unload frm ' deactivate the form
Set frm = Nothing ' remove from memory
Next
And when you do it properly the events fired in order are:
Form_QueryUnload
Form_Unload
Terminate
'
and to go a little further in....
the UnloadMode variable in the QueryUnload routine has the following values.
'0 : Closed from the X in the corner of the window.
'1 : Code in application has been invoked and is closing the application.
'2 : Windows session is ending.
'3 : Task Manager.
'4 : MDI Parent form is closing so child forms close to.
Last edited by Paul M; Aug 3rd, 2007 at 03:05 AM.
-
Aug 3rd, 2007, 06:52 AM
#14
Re: exit
Also on another note those who say ExitProcess is good. It is but again like End it skips the routines .......
Form_QueryUnload
Form_Unload
Terminate
-
Aug 3rd, 2007, 07:51 AM
#15
Re: exit
Yes true but as i said the order of firing is.....
QueryUnload
Unload
Terminate
So what it would be doing is skipping QueryUnload first then once it gets to unload going back to complete QueryUnload. But yea it is no big deal thats just my logic.
-
Aug 3rd, 2007, 08:10 AM
#16
Re: exit
 Originally Posted by Hell-Lord
Yes true  but as i said the order of firing is.....
QueryUnload
Unload
Terminate
So what it would be doing is skipping QueryUnload first then once it gets to unload going back to complete QueryUnload. But yea it is no big deal  thats just my logic.
Adding my code in the Unload event has no effect on the number of times or the order in which QueryUnload, Unload and Terminate are executed.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|