XL: Controls in Dialog Box May "Snap" to Preset Values

ID: Q111899

The information in this article applies to:

SYMPTOM

In Microsoft Excel, when you use Visual Basic commands to adjust the dimensions of controls in custom dialog boxes by manipulating the Top, Left, Height, and Width properties of the controls, the controls will always "snap" to certain numbers. For example, if you change the Left property of a control, it will always snap to a multiple of 0.75.

CAUSE

This behavior occurs because of the design of the custom dialog box system.

MORE INFORMATION

"Snapping" occurs when a control is placed in a certain position that is not precisely supported in a custom dialog box. For example, the Left property of a control always snaps to the next lowest multiple of 0.75. If you change the Left property of a control to any of the following values, the Left property will then snap to the indicated value:

   When <Control>.Left        It snaps
   is set to this value       to this value
   ----------------------------------------

   0                          0
   0.25                       0
   0.50                       0
   0.75                       0.75
   1.00                       0.75
   1.25                       0.75
   1.50                       1.50

If you try to set the Left property of a control to 1.12 or 1.25, for example, Microsoft Excel will snap the control's Left property to 0.75, because that is the greatest multiple of 0.75 less than or equal to 1.12 or 1.25.

In addition, if one property is changed by snapping, it may affect other related properties. For example, in the Visual Basic code example below, an edit box is initially created with a Top value of 50 and a Height value of 15. The Top value initially snaps to 49.5; however, when the Height value snaps, the Top value is changed to 47.25, even though 49.5 is a valid Top value. The Left and Width values are similarly related. The final Top, Left, Height, and Width values will always be valid, but you may observe controls moving slightly because of snapping.

Listed here are the guidelines that Microsoft Excel uses when determining how a property of a control will be snapped.

Note: The values may vary slightly in Microsoft Excel 97 from those listed below, but the behavior is the same.

   Control Type       Units Snapped to
   -----------------------------------------------------------------------
   ALL CONTROLS       The Left property of all controls snaps to the
                      nearest multiple of 0.75 less than or equal to the
                      indicated value.

                      The Top property of all controls snaps to the
                      nearest multiple of 0.75 less than or equal to the
                      indicated value.

   Edit Boxes         The Height property of edit boxes and labels use
   and Labels         the following table to determine the true Height
                      value:

                      Height Set to   Height Snaps to
                      -------------------------------
                      0.00  - 18.50   13.50
                      18.75 - 28.25   23.25
                      28.50 - 38.00   33.00
                      38.25 - 47.75   42.75

                      For subsequent ranges, add 9.75 for each range.

                      The Width property of edit boxes and labels snaps to
                      the nearest multiple of 0.75 less than or equal to
                      the indicated value.

   Buttons            The Height property of buttons snaps to the nearest
                      multiple of 0.75 less than or equal to the indicated
                      value, but no less than 15.75.

                      The Width property of buttons snaps to the nearest
                      multiple of 0.75 less than or equal to the indicated
                      value, but no less than 3.00.

   Dialog Box Frame   All properties of a dialog frame snap to the nearest
                      multiple of 0.75 less than or equal to the indicated
                      value.

   Group Boxes        The Height property of group boxes snaps to the
                      nearest multiple of .75 less than or equal to the
                      indicated value, but no less than 18.75.

                      The Width property of group boxes snaps to the
                      nearest multiple of 0.75 less than or equal to the
                      indicated value, but no less than 23.25.

   Check Boxes        The Height property of check boxes is always 16.50.

                      The Width property of check boxes snaps to the
                      nearest multiple of 0.75 less than or equal to the
                      indicated value, but no less than 23.25.

   Option Buttons     The Height property of option buttons is always
                      16.50.

                      The Width property of option buttons snaps to the
                      nearest multiple of 0.75 less than or equal to the
                      indicated value, but no less than 23.25.

   List Boxes         The Height property of list boxes uses the following
                      table to determine the true Height value:

                      Height Set to   Height Snaps to
                      -------------------------------
                      0.00  - 26.75   21.75
                      27.00 - 36.50   31.50
                      36.75 - 46.25   41.25
                      46.50 - 56.00   51.00

                      For subsequent ranges, add 9.75 for each range.

                      The Width property of list boxes snaps to the
                      nearest multiple of 0.75 less than or equal to the
                      indicated value, but no less than 12.00.

   Drop-Down Lists    The Height property of drop-down lists is always
                      15.00.

                      The Width property of drop-down lists snaps to the
                      nearest multiple of 0.75 less than or equal to the
                      indicated value, but no less than 12.00.

   Drop-Down Edit     The Height property of drop-down edit boxes is always
   Boxes              13.50.

                      The Width property of drop-down edit boxes snaps to
                      the nearest multiple of 0.75 less than or equal to
                      the indicated value, but no less than 12.00.

   Scroll Bars        The Height property of scroll bars snaps to the
                      nearest multiple of 0.75 less than or equal to the
                      indicated value, but no less than 12.00.

                      The Width property of scroll bars is always 12.00.

   Spinners           The Height property of spinners snaps to the nearest
                      multiple of 0.75 less than or equal to the indicated
                      value, but no less than 13.50.

                      The Width property of spinners is always 9.00.

Visual Basic Code Example

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/

The following subroutine creates a new dialog sheet, adds an edit box to it, changes the Top, Left, Height, and Width properties of the edit box, and then displays what the various properties have snapped to.

'----------------------------------------------------------------------
Sub ControlSnapDemo()
   'Add a new dialog sheet to the active workbook.
   Set DemoDlg = ThisWorkbook.DialogSheets.Add
   'Add an edit box to the current dialog sheet. The dimensions
   'supplied are arbitrary.
   Set EdBox = DemoDlg.EditBoxes.Add(50, 50, 50, 50)
   'Set the Top, Left, Height, and Width properties of the edit box.
   EdBox.Top = 50
   EdBox.Left = 70
   EdBox.Height = 15
   EdBox.Width = 80
   'Construct a message string which will be shown in a message box.
   'Chr$(9) is a tab character: Chr$(10) is a line feed.
   MsgString = Chr$(9) & "Original Setting" & Chr$(9) & "Snaps To"
   MsgString = MsgString & Chr$(10) & "Top" & Chr$(9) & "50" & Chr$(9)
   MsgString = MsgString & Chr$(9) & EdBox.Top & Chr$(10) & "Left"
   MsgString = MsgString & Chr$(9) & "70" & Chr$(9) & Chr$(9)
   MsgString = MsgString & EdBox.Left & Chr$(10)& "Height" & Chr$(9)
   MsgString = MsgString & "15" & Chr$(9) & Chr$(9) & EdBox.Height
   MsgString = MsgString & Chr$(10) & "Width" & Chr$(9) & "80" & Chr$(9)
   MsgString = MsgString & Chr$(9) & EdBox.Width
   'Show the message.
   MsgBox MsgString
End Sub
'----------------------------------------------------------------------

When you run this subroutine, a new dialog sheet containing a new edit box will be created. A message box will appear with this message:

            Original Setting   Snaps To
   Top      50                 47.25
   Left     70                 69.75
   Height   15                 13.5
   Width    80                 79.5

The "snap to" values in Microsoft Excel 97 may vary slightly from the "snap to" values in Microsoft Excel 5.x and 7.x. The "snap to" values in Microsoft Excel 97 will tend to vary less from the original settings than previous versions of Microsoft Excel.

The properties of the edit box have snapped to values appropriate for an edit box. Note that the Top property has been "over-snapped" because of the substantial change in the Height property.

Additional query words: 5.00 7.00 97 xl97

Keywords          : kbprg kbdta KbVBA 
Version           : WINDOWS:5.0,5.0c,7.0,7.0a,97
Platform          : WINDOWS

Last Reviewed: May 17, 1999