Hi folks,

I'm wondering how to close a VB6 multiform application using the red x on the main form. There should be a message box that lets the user confirm the exit ( yes ), or ( no ) resume the application.

Thank You!

Dani AI

Generated

's QueryUnload approach is the right starting point: the QueryUnload event runs before Unload and gives you a way to cancel closing (so the red X can be intercepted). 's suggestion to mark the thread solved is fine once the code works — but a few refinements help make the behavior robust in real-world multi-form apps.

Key points to harden the solution without repeating the code already posted:

  • Use QueryUnload on the main form so you can detect the reason the form is closing and cancel the close if the user says "No". QueryUnload gives a Cancel parameter that prevents Unload from continuing.
  • Differentiate user close (red X / Control menu) from programmatic Unload and Windows shutdown by checking the unload reason before prompting. That avoids asking twice when your code or the OS is already closing things.
  • Centralize cleanup. Put resource-release logic (files, DB connections, timers, COM objects) in a single module routine or in each form's Unload, and call that routine when the user confirms exit. Avoid using End because it stops cleanup code and can leave resources locked.
  • Handle modal or topmost windows: message boxes can be hidden behind modal dialogs. Either close/hide modal child forms before prompting, or present a modal confirmation dialog owned by the main form so it stays on top.

Testing checklist:

  • Test clicking red X on the main form, Alt+F4, choosing an Exit menu item, programmatic Unload, and system shutdown — verify when the prompt appears and that Cancel actually prevents closing.
  • Add simple logging (temporary) in QueryUnload and Unload to verify the sequence and that each form gets a chance to clean up.
    These steps preserve the simple UX you want while avoiding leaks, race conditions, and hidden dialogs in multi-form VB6 applications.

Recommended Answers

All 2 Replies

Hi folks,

I'm wondering how to close a VB6 multiform application using the red x on the main form. There should be a message box that lets the user confirm the exit ( yes ), or ( no ) resume the application.

Thank You!

A nice way to close a form using the red x in a multi form application.

I found it myself. Hope this helps others.

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

' If message box conflicts with other forms.
frm_A_About.Visible = False
frm_B.Visible = False

' Only prompt if the user hits the red X
If UnloadMode = 0 Then
If MsgBox("Are you sure you want to quit?", vbYesNo Or vbQuestion) = vbNo Then Cancel = True
Unload frm_A
Unload frm_B
Unload frm_C
Unload frm_Z

Set frm_A = Nothing
Set frm_B = Nothing
Set frm_C = Nothing
Set frm_Z = Nothing
End If
End Sub

Good job TDuck, thats how you learn. Please mark it as Solved if your problem is solved.
thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.