PTS Blog

Main menu:

PTS Utilities

Commercial Utilities developed by Peltier Technical Services

  Waterfall Chart

Excel Books

Books that I own and use while developing in Excel

Goods and Services

Excel or charting related products and services which I use or feel are worthwhile additions

Subscribe

Site search


Recent Posts

Recently Commented

March 2008
S M T W T F S
« Feb   Apr »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Archive


 

Categories


 

Repurpose the Red X Close Button on a VBA UserForm

by Jon Peltier
Peltier Technical Services, Inc., Copyright © 2008. All rights reserved.

A frequently asked question is "How do I hide the red X close button on the corner of a VBA user form?"

My answer is that you shouldn't hide the red button, since the user knows what it does, or at least what it's supposed to do. Leave the button in place as an obvious way to escape the running procedure. It's your job as programmer to make the escape route work both for the user and for the program.

VBA UserForm with Red X Close Button

This userform has code behind the OK and Cancel buttons to hide the form, which keeps it resident in memory. This way, the calling code can interact with the form after it has been dismissed.

The default behavior of the red X in the top right corner is to unload the form, that is, clear it from memory. This is not good, because the calling code will be unable to communicate with the form.

This form has code that repurposes the red X, turning a click on that button to a click on the form's Close button. This is what the user wanted to do anyway.

There are more detailed methods which rely on Windows API calls that will actually disable or hide the red X button, but I don't like to take away a piece of the interface that the user understands.

The code in the userform is shown below. The variable mbCancel is true if the dialog is dismissed with the Cancel button, or false if it is dismissed with the OK button.

Visual Basic:
  1. Option Explicit
  2.  
  3. Dim mbCancel As Boolean
  4.  
  5. Private Sub btnCancel_Click()
  6.     mbCancel = True
  7.     Me.Hide
  8. End Sub
  9.  
  10. Private Sub btnOK_Click()
  11.     mbCancel = False
  12.     Me.Hide
  13. End Sub
  14.  
  15. Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
  16.     ' how was the form closed?
  17.     ' vbFormControlMenu = X in corner of title bar
  18.     If CloseMode = vbFormControlMenu Then
  19.         ' cancel normal X button behavior
  20.         Cancel = True
  21.  
  22.         ' run code for click of Cancel button
  23.         btnCancel_Click
  24.     End If
  25. End Sub

Share/Save/Bookmark

Comments

Comment from Dick Kusleika
Time: Tuesday, April 1, 2008, 8:56 am

Do you always mimic the Cancel button? For instance, what if you have user interaction on the form? If a user presses Cancel, they should expect that the form closes and nothing is saved. If the user pressed OK, they should expect that the form closes and everything is saved. If the user presses the red x, however, I think they expect to be warned if the form is dirty. That is, Close and Cancel have different connotations.
I usually program for that with an IsDirty module-level variable, but I find that extra work as distasteful as the code that hides the red x.


Comment from Jon Peltier
Time: Tuesday, April 1, 2008, 9:21 am

Hi Dick -

I don’t *always* mimic the Cancel button, but that’s what usually makes the most sense in my usage. The user decides not to graph that data, or create that pivot table, or export that report, and Cancel fits best of all. If unsaved data is an issue, I may be checking in the Cancel button code anyway. Or my buttons will be labeled “Save”, “Save and Exit”, and “Exit Without Saving”. Best practices include using good, descriptive control labels.

In my experience, it seems to me that the red X is for the user who says “let me the hell out of here!”, so I generally just use Cancel.


Write a comment





Create Excel dashboards quickly with Plug-N-Play reports.