A GOTCHA! to avoid with Modal Forms

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Anthony

    #1

    A GOTCHA! to avoid with Modal Forms

    I recently discovered an interesting aspect of Forms which are launched as
    modal forms via ShowDialog.

    As it turns out, if the modal form is visible and its Hide( ) method is
    performed it actually fires the Close( ) event. This makes sense, since a
    modal form only allows further execution of code past the line the launched
    it when it goes away. Hence, hiding it is the same as it goes away, since
    hiding it would 'kidnap' the application ala it's being modal, and thus
    calls close.

    However, one should also note that Close( ) doesn't actually close the form,
    but Hide( )'s it! That's so it can be used immediately without setting it
    up again (thus very fast).

    So, to re-cap (in an unrigorous way), Hide( ) becomes Close( ), and they
    both just do Hide( ), but they both call the Close handler.

    The reason this caught me by surprise is that I had created a special Close
    event handler that exited the entire program after fading, and was a bit
    shocked at first to find my program exiting when I just hid the modal form!
    FYI, I corrected by deriving a new Form class from the standard one that
    changed the Close handler to reject closing (i.e., e->Cancel = true). Now it
    just hides the modal form when it is hidden!


Working...