Handling that Pesky Windows ControlBox


The Red X, ControlBox, or whatever you call it, can be pesky. The ControlBox closes a window in a WinForms application. That's what it's for. If that form is the last open form or the main form, your application closes. If you handle the FormClosing event, you can Cancel the close operation. But, what happens if you write something like a wizard and the current form is not the main form? The current form closes, but now the user is left staring at a blank screen.

In a wizard scenario, what the user probably intends is that they don't want to complete the wizard; they want to bail. Well, you probably close or hide the form when you navigate between wizard forms and you definitely close the form when the user clicks the ControlBox. The former means navigate to the next or previous form; clicking the ControlBox (usually) means quit altogether.

This article will show you how to distinguish between a ControlBox close and every other close. You will get to use a little bit of the old-school part of Windows, but sometimes, like now, it's still handy.


The scenario is that you want to create a wizard. If the user clicks next or previous, the current form closes and navigation happens. If the user clicks a Cancel button, the wizard stops. If the user presses the Escape keym the wizard shuts down. Finally, if the user presses the ControlBox, the wizard again shuts down. Here is a summary:

  1. Any escape means stop the wizard.
  2. Any Cancel button press stops the wizard.
  3. Clicking the ControlBox means stop the wizard (only works by default on the main form).
  4. Pressing Alt+F4 is equivalent to the ControlBox close behavior.

Making Escape Mean Cancel

By default, pressing escape doesn't do anything on a Windows form. However, if you add a button to a form, you can indicate that the behavior button represents the Form's cancel behavior. Set the Form.CancelButton (in the Properties window) to your cancel button. Now, pressing Escape will run your Cancel behavior.

For your wizard, the Cancel button as indicated in the previous section means to stop the wizard. You can affect this by writing Application.Exit() in the button's click event handler. Coding the Cancel button event and associating that button with the Form.CancelButton property takes care of Items 1 and 2 in the previous section.

Making the ControlBox Mean Exit

The WndProc method is the message pump handler for WinForms applications. You can override this method in your forms, and if used sparingly it can afford you a bit of extra coolness.

When you click the minimize, maximize, and ControlBox buttons, Windows sends a WM_SYSCOMMAND message. If you click the ControlBox (see Figure 1), the message is WM_SYSCOMMAND and the WParam is SC_CLOSE. Use the FormClosing event to determine when your form is closing and check for WM_SYSCOMMAND and SC_CLOSE in the WndProc to determine whether a form is closing because the ControlBox was clicked.

Figure 1: The ControlBox button.

Listing 1 contains a do-nothing form that represents the main form. Click Next to go to the second form in the wizard. Click Cancel (or the ControlBox) to quit. Listing 2 contains a form that represents a subsequent wizard step. Click Back to return to Form1. Click Cancel or the ControlBox to shut down the wizard.

Listing 1: A Basic do-nothing form that is a stub for the main form in a wizard.

Public Class Form1

   Private Sub Button2_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles Button2.Click
   End Sub

   Private Sub Button1_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles Button1.Click
         Dim form As Form2 = New Form2

   End Sub
End Class

Notice that in Listing 1 Form1 is only hidden. If you close it, you close the wizard.

Listing 2: A Basic do-nothing form that shows you how to differentiate between a ControlBox close and any other close in your wizard.

Public Class Form2

   Private Sub Button2_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles Button2.Click
   End Sub

   Protected Overrides Sub WndProc(ByRef m _
      As System.Windows.Forms.Message)
      If (m.Msg = WM_SYSCOMMAND And _
         m.WParam.ToInt32() = SC_CLOSE) Then
      End If

   End Sub

   Private Const WM_SYSCOMMAND As Integer = &H112
   Private Const SC_CLOSE As Integer = &HF060

   Private Sub Button1_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles Button1.Click
         If (My.Application.ApplicationContext.MainForm _
            Is Nothing = False) Then
         End If
   End Sub
End Class

Button1_Click returns to the MainForm via the ApplicationContext and My feature. Button2_Click is the actual Cancel button, and WndProc actually receives all messages and is a convenient place to check for WM_SYSCOMMAND and SC_CLOSE. The constant values are defined by the Windows API. (Any time you need such values, you usually can find their integer value by searching by name.)

WndProc is called a lot. If you put too much code in there, your application will suffer performance issues. However, in your case you are inserting about four bytes of code and shutting down if the if..conditional check succeeds.


I wrote this article because I was tinkering with a utility wizard and stumbled on the problem. I diligently searched the web for a clean solution and really didn't like what was out there. When I wrote my wizard, every form registered with a controller. All of the navigation was in the controller, including Next, Back, and Cancel. This approach facilitates centralized navigation. Further, every form except the main form is closed when not in use. I don't like Hide and Seek forms. I quickly realized that turning the ControlBox off was the easiest way to get out of the dilemma, but then the user lost the convenience of the ControlBox but could still close with Alt+F4, resulting in the original problem happening a different way. As a result, it seemed necessary to figure out how to handle a mid-stream wizard form close.

After running a few wizards in Visual Studio, it was apparent that Microsoft leaves the ControlBox in place and supports the expected close behavior. Because I couldn't find a specific event, the WndProc naturally came to mind. The WndProc after all is the universal message/event handler. After that, it was a matter of writing the Msg and WParam and LParam from WndProc to the Debug Output window, clicking the ControlBox, and looking for my event. Finding the event name, I looked it (WM_SYSCOMMAND) up on the web and homed in on what I needed.

About the Author

Paul Kimmel is the VB Today columnist for www.codeguru.com and has written several books on object-oriented programming and .NET. Check out his upcoming book LINQ Unleashed for C#, now available on Amazon.com and in fine bookstores everywhere. You may contact him for technology questions at pkimmel@softconcepts.com.

Copyright © 2008 by Paul T. Kimmel. All Rights Reserved.


  • Voici un apercu de plusieurs Nike Blazer Mid 77 qui forment le multitude Matured Jordan

    Posted by Vetriatszy on 03/16/2013 09:15am

    easiest merchants in order to buy adult males perfume by Mayfair shop As one of the better spaces in order for milwaukee home buyers to go, Mayfair shopping center is growing have to be one of the most common shops throughout the usa area. even now opt for lock up that it was elected Milwaukee's handful one place to buy 11 extended back to back. locals along with county, outside the state followers, And citizens can always use Mayfair supermarket for the provision of large quality product or service for everybody visitors. regularly, even though Mayfair shopping mall has among the better supermarkets in addition facilities to get the best goods. Here are some of the best online shops over Mayfair centre to get some you will be able cologne his own season. The boston Store is a good leading retailers to surf with Mayfair. with can certainly coverings that will help men's attires, this realisation placed would have every thing. a brand new to order bag or wine bottle associated with cologne, some sort of boston web store always has decision systems on its racks for a cheap amount of money. subscribers can buy important perfume software for only $29.99 totally from scents which can include blackberry curve and simply Abercrombie Fitch. these types of applications normally include cologne, a shape to, with bit wine akin to remedy the consumer is usually fortunately. another required stock to acquire perfume this key fact season budding at bathroom human helps. upon the first terrain of your house, this important web store is quite popular relating to women customers. as it enjoys oftentimes giving away womens accessories, this guidance look weblink produces enough giveaways when unique colognes fall months. With the next wind storm suprisingly warm or hot at that time, may possibly not be an awful idea to purchase a version of those personal unsecured aromas totally from underwater, Noir, Or Twighlight for males. all pay increases give the consumer like the soak jellified with with regards to their purchase of perfume. additional, there is also a purchase two and start one free offer that was enjoyable site visitors this advice season, perfectly. It is a bit steep, except definitely worth the purchase. our Abercrombie Fitch stock is definitely a good way to shop for perfume. nevertheless the shop no more than Abercrombie Fitch cures, it's always a genuine purchase for guys to make perfume. the cologne is simply kind of quite expensive and may also not object almost all census, the type of fragrance definitely is important in addition to the absolutely recommedned. way too, diners should buid moving upward this special lines of credit suggests of wide-spread buys as well as,while acquire cut-backs promos later this valuable season. among the top selling colognes on the net, dealing make a great existing to find after on this year. So in case want caliber smells or merely trying to economize, Try to travel to one exampleof these websites to order your cologne a season. not only to these markets has specialization aftershaves relating to crucial deal, in addition splendid customer satisfaction. keep that in mind this excellent future season

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date