Results 1 to 11 of 11

Thread: [RESOLVED] Why form still visible when 'unload'

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2017
    Posts
    79

    Resolved [RESOLVED] Why form still visible when 'unload'

    Hi every one there,

    Code:
    Private Sub Command1_Click()
    If Text2.Text = IDS(0) Then
    
        Main.Show vbModal
        Unload Form1
    End If
    
    End Sub
    The form is unloaded but still visible behind Main Form Why?
    Where as in some cases i found when unloaded a form its closed.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Why form still visible when 'unload'

    The Unload statement is not executed until the modal form is closed or hidden. Remove the vbModal part and Form1 should unload after Main is shown.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2017
    Posts
    79

    Re: Why form still visible when 'unload'

    Thank you Lav....
    Your reply solved the problem....
    Can i get some extra knowledge .. Why that one not happening earlier... what is the role of vbModal / vbModeless.

    Thank you again..

  4. #4
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: [RESOLVED] Why form still visible when 'unload'

    F1 or, look for vbModal in Help

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: [RESOLVED] Why form still visible when 'unload'

    In my signature is a link to VB help files online. You may want to bookmark that link.

    Using that link, if you navigated to: Methods > S > Show > Show Method, you would find this on that page
    When Show displays a modeless form, subsequent code is executed as it's encountered. When Show displays a modal form, no subsequent code is executed until the form is hidden or unloaded.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,936

    Re: [RESOLVED] Why form still visible when 'unload'

    Modal forms actually can be a bit of a mystery at times. However, I suspect many of those mysteries are beyond what Urgentbody is asking.

    To fully appreciate them, we must recognize that showing a form as modal involves both Code consideration as well as UI consideration. As we know, when we show a form modally, whatever code did that is suspended at that line of code until that modal form is made non-modal in some way (unloaded, shown non-modally, hidden), at which point the original code will continue to execute once the thread is handed back to Windows.

    Why do I talk about the thread? Well, because, we might hide our modal form, do some operations, and then re-show it as modal while not releasing the thread. This would keep the original code (that showed the modal form) suspended. And, as a further note, this is one place where DoEvents can cause havoc. For instance, if you have a form loaded modally, it temporarily hides itself, executes some DoEvents, and then re-loads itself modally, you will get bizarre results (with the code loading the modal form continuing to execute in some cases).

    Another thing to appreciate about modal forms is that, you can unload (but not hide) their parents (forms lower in the ZOrder) and you will also (somewhat automatically) unload these modal forms. Personally, I would consider this coding practice highly inadvisable. In fact, that seems to be precisely what Urgentbody is doing. I can set up test cases where bizarre things happen when you do this, especially if the "Parent" option is used when loading the modal form. In some cases, you can actually crash the IDE.

    Now, if we try to hide (not unload) a form that's lower in the ZOrder when a modal form is showing, we get an error:
    Name:  ModalError.png
Views: 170
Size:  4.2 KB

    This is expected (at least by me). And it's probably the way unloading a form lower in the ZOrder should work, but doesn't.

    Also, there's the issue of what events are still allowed to fire within the COM/Code object of forms lower in the ZOrder when a modal form is showing. And there seem to be inconsistencies here between the IDE and compiled. For the most part, all keyboard and mouse related events are prohibited. However, other non-mouse/non-keyboard events are often still allowed to fire.

    And, if you directly call it (because it's declared as Friend or Public), you still have full access to the procedures in the COM/Code objects of forms lower in the ZOrder. You also have access to all these "lower" forms' controls if you directly access them. In other words, a modal form can easily change captions, lists, or whatever on some parent's form.

    Also, it's a form's UI, and not its COM/Code piece, that defines it as modal (or not). However, again, this affects (suspends) upstream code execution.

    Personally, because of these ... ummm ... confusions, I stay away from modal forms execept for MsgBox messages and my own MsgBox-type messages. However, this does mean that I must do an excellent job of managing my user-interface if/when multiple forms are showing. In most cases, I just hide parent forms such that only one form is showing at a time (but this isn't always practical). Also, this means that I must manage my code better because there won't be any code suspended anywhere.

    Just some personal thoughts about this.

    Best Regards,
    Elroy

    EDIT1: Just one last comment. I find it rather bizarre that some particular form's state (modal or not) can affect whether or not code can execute (i.e., because it's suspended because it loaded a modal form). It's almost like calling a function (where the caller is suspended until the function returns), but it's not. A modal form returns the thread to Windows the way any other form does, whereas a called function doesn't return the thread until it's completed its task.
    Last edited by Elroy; Oct 14th, 2017 at 11:52 AM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2017
    Posts
    79

    Re: [RESOLVED] Why form still visible when 'unload'

    Thank you Elroy and LaVolpe for the information.

  8. #8
    Lively Member
    Join Date
    Jun 2016
    Posts
    109

    Re: [RESOLVED] Why form still visible when 'unload'

    Quote Originally Posted by LaVolpe View Post
    When Show displays a modeless form, subsequent code is executed as it's encountered. When Show displays a modal form, no subsequent code is executed until the form is hidden or unloaded.
    ... well, I think I understand that, but *why* a modal form is not automatically the topmost??

    Now all too often I happen to open a modal form and it is hidden below an other one.
    And all to often the Ctrl-Alt-Del is the only escape

  9. #9
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,936

    Re: [RESOLVED] Why form still visible when 'unload'

    Say Tubus,

    If possible, it'd be nice if you could pull together a small example of what you're talking about. I've seen what you describe happen when a form is manipulated with some API calls, but I'm not sure I've seen it happen under normal circumstances. Within a particular program/project, the last form to be loaded modally will/should be highest (most in front) in the ZOrder.

    Now, let's not forget that there can be a hierarchy to modally loaded form. In other words, some code may load a modal form. And then, that form may load subsequent modal forms. Etcetera. That's why, above, I say the "last form" to be loaded modally.

    Best Regards,
    Elroy

    EDIT1: Also, let's remember that each "instance" (i.e., program) is going to have its own ZOrder list. In other words, another application can still place windows over your program's modal windows (unless you make an API call to make them always-on-top, and even then, the always-on-top list still has its own ZOrder list to keep track of all the always-on-top forms).
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  10. #10
    Lively Member
    Join Date
    Jun 2016
    Posts
    109

    Re: [RESOLVED] Why form still visible when 'unload'

    Quote Originally Posted by Elroy View Post
    I've seen what you describe happen when a form is manipulated with some API calls, but I'm not sure I've seen it happen under normal circumstances.
    Thanks Elroy for your answer

    ...well yes, I do manipulate the Z-order with the respective API calls, I need that to display sort of monitoring or toolbox windows above the main windows.

    To be fair, it's not a problem in the compiled exe, I've learned to handle this, but often frustrating in the IDE. An unexspected error message popping up, but hidden behind a topmost-by-API window, then you are locked. No way to bring it back to the front or move focus back to it.

    I would think, if Windows restricts User-input to a certain window, it should bring it to the top. Period.

  11. #11
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: [RESOLVED] Why form still visible when 'unload'

    If you're in the IDE, I would think you could hit Ctrl-Break to pause the executing program and in the immediate window use the move method of the form in question (or use it's left and top properties) to move it to where you can see it.

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