|
-
Feb 25th, 2003, 10:38 AM
#1
Thread Starter
Frenzied Member
VB - Forms - Unload a form in the Form_Load() event
If you putthe command "Unload me" in the VB Form_Load() event it generates a runtime error "The object was unloaded".
Instead use the Postmessage API to post a WM_CLOSE message to it...
VB Code:
Option Explicit
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_CLOSE = &H10
Private Sub Form_Load()
PostMessage Me.hwnd, WM_CLOSE, 0, 0
End Sub
-
Mar 3rd, 2003, 04:34 AM
#2
Addicted Member
Nice!
...
Have a nice day, you ApiGuru
Special thanks to some wonderful people,
such as Lothar the Great Haensler, Aaron Young,
dr_Michael, Chris Eastwood, TheOnlyOne ClearCode....
-
Mar 3rd, 2003, 08:38 AM
#3
What's the purpose of unloading a form that's being loaded? I'm just curious... In what kind of task would that be useful?
-
Mar 3rd, 2003, 08:50 AM
#4
Thread Starter
Frenzied Member
For example, in the Microsoft Template "Startup Tips" form, if you have a registry entry "Show tips at startup" set to false the form just unloads itself...of course MS didn't test their code so in fact you get a runtime error *sigh*
-
Mar 4th, 2003, 05:59 PM
#5
PowerPoster
Well
I have always accomplished this with a timer. Good piece of code.
Remaining quiet down here !!!
BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....
-
May 24th, 2003, 07:28 AM
#6
James, do you do something like this(just wondering)?
VB Code:
Dim Whatever As Boolean
Private Sub Form_Load()
Timer1.Interval = 100
Timer1.Enabled = False
'For some reason Whatever is True
Whatever = True
If Whatever = True Then
Timer1.Enabled = True
End If
End Sub
Private Sub Timer1_Timer()
Unload Me
End Sub
Has someone helped you? Then you can Rate their helpful post. 
-
Jun 3rd, 2003, 02:39 AM
#7
New Member
What is the use
Unload Me command in Form_Load() does not give any error. So what is the point in using this lengthy code????
-
Jun 3rd, 2003, 04:23 AM
#8
Re: What is the use
Originally posted by lilford
Unload Me command in Form_Load() does not give any error. So what is the point in using this lengthy code????
He's right, I have tested this in VB5 and VB6 and no error pops up.
How about VB.Net?
-
Jun 3rd, 2003, 04:40 AM
#9
Yes - well if you test this by showing the form from code rather than just having it as the startup form it raises runtime error '364' Object Was Unloaded.
e.g. in Form_1 Load:
VB Code:
Option Explicit
Private Sub Form_Load()
Form2.Show
End Sub
and in Form2:
VB Code:
Option Explicit
Private Sub Form_Load()
Unload Me
End Sub
-
Jun 4th, 2003, 02:54 AM
#10
New Member
You r rite buddy. it does raise the error and the code works fine..Thumbs up buddy.
-
Jun 4th, 2003, 03:14 AM
#11
Fanatic Member
The code works extremely well. Tried it with form unload-worked as well.
A.A. Fussy
Babya Software Group
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
|