Results 1 to 11 of 11

Thread: VB - Forms - Unload a form in the Form_Load() event

  1. #1

    Thread Starter
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616

    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:
    1. Option Explicit
    2.  
    3. 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
    4. Private Const WM_CLOSE = &H10
    5.  
    6. Private Sub Form_Load()
    7.  
    8. PostMessage Me.hwnd, WM_CLOSE, 0, 0
    9.  
    10. End Sub
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  2. #2
    Addicted Member Cimperiali's Avatar
    Join Date
    Oct 2002
    Location
    Milan, Italy, Europe
    Posts
    188

    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....

  3. #3
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    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?

  4. #4

    Thread Starter
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    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*
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  5. #5
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    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....

  6. #6
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    James, do you do something like this(just wondering)?

    VB Code:
    1. Dim Whatever As Boolean
    2.  
    3. Private Sub Form_Load()
    4. Timer1.Interval = 100
    5. Timer1.Enabled = False
    6. 'For some reason Whatever is True
    7. Whatever = True
    8.  
    9. If Whatever = True Then
    10.   Timer1.Enabled = True
    11. End If
    12. End Sub
    13.  
    14. Private Sub Timer1_Timer()
    15. Unload Me
    16. End Sub


    Has someone helped you? Then you can Rate their helpful post.

  7. #7
    New Member
    Join Date
    Aug 2002
    Location
    India
    Posts
    7

    Question 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????

  8. #8
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    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?

  9. #9
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148
    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:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.  
    5. Form2.Show
    6.  
    7. End Sub
    and in Form2:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.  
    5. Unload Me
    6.  
    7. End Sub

  10. #10
    New Member
    Join Date
    Aug 2002
    Location
    India
    Posts
    7

    Thumbs up

    You r rite buddy. it does raise the error and the code works fine..Thumbs up buddy.

  11. #11
    Fanatic Member
    Join Date
    Jul 2002
    Location
    Australia
    Posts
    635
    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
  •  



Click Here to Expand Forum to Full Width