The Close Window Button

I'm working in VB5. In my program I have a menu item to close the program but it checks to see if I want to save my data before it closes. How can I make the the close window button do the same before closing the program?


  • Hi giftedsailor,

    Sorry I can't answer your question exactly. I've looked through several VB books that I have, including Microsoft's own, and none of them mention how the caption bar icons are treated.

    Without loading VB5 (which I haven't done for many years), I can't check it out - but if you click on the Form to bring up the code editor, you will probably find the event code for Private Sub Form_Load().

    Somewhere around there you might be able to spot an option to set up an event handler for when the Form_Close is clicked - then you could place your shut down code in that routine.

    Certainly, when you click the 'X' icon, a message will be sent to Windows which you should be able to intercept.

    In the Basic I'm currently using, you would use ..

    sub msghandler
    select @class
    case @idclosewindow
    ' closing the window
    .... whatever code you wish to execute ...
    closewindow win

    One of the problems with GUI systems like VB and RealBasic, is they don't let you get at all that's going on under the hood in Windows.

    I like to have access to all of it, as in the bit of code above.

    all the best,


  • Thanks for the tip. After trying several that seemed likely I finally found that [b]Form > QueryUnload[/b] was the answer.

    Thanks again
  • Ah, of course - not something that springs readily to mind .. and not even mentioned in several of my books.

    It is in the Microsoft literature though, with the caveat that ..

    'Important: Under certain circumstances, a form will not receive a QueryUnload event. If you use the End statement to terminate your program, or if you click the End button (or select End from the Run menu) in the development environment.'

    Glad you found a solution.

    Best wishes,


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!