CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Dec 2000
    Location
    Los Angeles, CA
    Posts
    34

    Unable to Unload

    I ran into an error I had never heard of before.

    Run-time error '365'
    Unable to unload within this context.

    MSDN says this is because I have an Unload within the Change, Click or Drop-Down event of a combobox. I admit I do have an unload in the click event of a combobox, but what is so wrong with that?!?!?! I am trying to make a fairly intuitive UI that allows a user to make a series of choices in a combo box. Whenever the final decision is made, a msgbox pops up to confirm the choice. If they click "OK", then the form needs to unload. So where the heck else am I supposed to put the Unload event?

    To get the functionality I want I don't see any way to have my Unload outside of the scope of the combobox_click event...

    Anyone ever run into this before? Or any suggestions on how to minimize the changes to my UI and be able to Unload the form?

    Thanks for the help...

    Nathan



  2. #2
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: Unable to Unload

    Wow, sure it is, I had never encountered before.

    You can use the End statement instead of the unload event. But this would not be a good solution and would cause problems, because the From_Unload event wouldn't be fired.

    However, I got a workaround and here it is.


    private Sub Combo1_Click()
    If Combo1.Text = "exit" then SendKeys "%{F4}"
    End Sub

    private Sub Form_Unload(Cancel as Integer)
    MsgBox "Hi! Got a workaround"
    End Sub




    It relies on sending the Alt-F4 combination to the application.



  3. #3
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Unable to Unload

    Shree solution is clean (only a pity I have already spent all my votes, today, or here he would have take a ten for his workaround). However, there is another common solution: put a timer in your form and, with 50 ms of delay, you can unload in timer event. On click event in combobox, enable the timer (which first statement should be: timer1.enabled = false). Shree solution is still better.


    Special thanks to Lothar "the Great" Haensler. Come back soon, you Guru.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  4. #4
    Join Date
    Dec 2000
    Location
    Los Angeles, CA
    Posts
    34

    Re: Unable to Unload

    What a great, simple work around! I think, however, the enabling a timer is a better solution for this situation. The form that I am needed to "unload" is not the "main" form of my application. It is loaded modally and has no title bar or "X" button, because the border style is set to "0-None". I tested typing ALT+F4 from my application with the form I needed to unload open, and is did nothing.

    Under more "normal" cercumstances, your solution would have worked. But in this case the timer is the only solution that doesn't involve redesigning the UI.

    Thanks for the input.

    Nathan


  5. #5
    Join Date
    Dec 2000
    Location
    Los Angeles, CA
    Posts
    34

    Re: Unable to Unload

    This time you get the 10 . The solution you present works better for this situation. Not that Shree should have known, because my inital post left out a lot of deatails that cause your solution to be better.

    But the timer is a more universal solution to this problem. It should work in any situaltion where Runtime-error '365' applies.

    Good show

    Nathan


  6. #6
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Unable to Unload

    Thanks both for rating and for have let me know it helped you.

    Special thanks to Lothar "the Great" Haensler. Come back soon, you Guru.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  7. #7
    Join Date
    Mar 2005
    Posts
    2

    Exclamation Re: Unable to Unload

    Hi ,

    I am facing the same problem in a different manner. I want to unload text boxes in combo box click event. I Put a timer in the form and invoked the timer in the combobox click event.

    But what happens is that the timer event is fired after the completing the combo box click event. So i am unable to unload the text boxes. Please help me to solve this problem.

    Thanks and Regards,

    P.Gerald Manickam

  8. #8
    Join Date
    Aug 2004
    Location
    Bucharest, Romania... sometimes
    Posts
    1,039

    Re: Unable to Unload

    Check out Jim Perry's solution in this thread:
    http://forums.microsoft.com/msdn/sho...92941&siteid=1

    Basically, what I would recommend is not sending a keystroke since you can't be sure it will be processed by the expected window...
    Setting a timer will achieve the expected result, but too many timers in the same process or to the same window will make control flow quite random, thus not so good as a general solution.
    Since you can't unload the parent form while still processing the combo's event, you should try doing it immediately after. How? If not SendMessage, which is what Unload is probably using, then PostMessage (which is what Me.Close suggested by Jim Perry is using).

    If you tell us more about those text boxes, combo boxes and forms you have (who's parent is who, modal / modeless, wanted order of unloading), we may be able to help more.
    Regards,
    Bogdan Apostol
    ESRI Developer Network

    Compilers demystified - Function pointers in Visual Basic 6.0
    Enables the use of function pointers in VB6 and shows how to embed native code in a VB application.

    Customize your R2H
    The unofficial board dedicated to ASUS R2H UMPC owners.

  9. #9
    Join Date
    Aug 2006
    Location
    Hubli, India
    Posts
    70

    Re: Unable to Unload

    May I Also Suggest

    If you are showing the Other Form Modally

    Then Dont Unload it , Just Hide it by setting me.visible = false or me.hide

    Then unload it in the Form from where you opened it.

    I mean

    in Form1

    Code:
    Private Sub Command1_Click()
     Form2.Show vbModal
     Unload Form2
    
    End Sub
    In Form2

    Code:
    Private Sub Combo1_Click()
    
    If Combo1.Text = "Close" Then
      Do Required Work
      Me.Hide
      Exit sub
    End If
    
    End Sub

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

Featured