PRB: Label Grows Wider When AutoSize and WordWrap Are True

Last reviewed: November 19, 1996
Article ID: Q135651
The information in this article applies to:
  • Standard, Professional, and Enterprise Editions of Microsoft Visual Basic, 16-bit and 32-bit, for Windows, version 4.0
  • Standard and Professional Editions of Microsoft Visual Basic for Windows, version 3.0

SYMPTOMS

As more text is added to a Label Control that has the AutoSize and WordWrap properties set to True, both the width and the height of the label increase. This is not what you might expect, which is that only the height of the label would increase.

WORKAROUND

Set the Width property back to the original width explicitly after adding text to the caption of the label.

STATUS

Microsoft is researching this behavior and will post new information here in the Microsoft Knowledge Base as it becomes available. This behavior has been modified in Visual Basic version 4.0. Increases in the width of the Label control after text is added to the caption are now significantly smaller than the increases seen in version 3.0, although they do still occur. Also, in version 4.0, the width of the label doesn't become larger and larger when you repeatedly add text to the caption. In Version 3.0, the width of the label continues increasing as more text is repeatedly added to the caption.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Start a new project. On Form1 (the default form), place a Label control (Label1) and a command button (Command1).

  2. Select the Label1 control on the form and press the F4 key to see the Properties window. Set WordWrap and AutoSize to True, and in order to see the problem clearly, set the BorderStyle to 1 (Fixed - Single). Set the Width to 1200 Twips (twips is the default ScaleMode).

  3. Double-click the Command1 button. Place the following code in the Command1_Click event procedure:

    MsgBox "Start Width=" & Label1.Width For i = 1 To 50

          Label1.Caption = Label1.Caption & "This is my Text "
    
    Next MsgBox "Final Width=" & Label1.Width

  4. Run the program by pressing the F5 key. Press the Command1 to see that the width of Label1 increases as more text is added to the Caption. In Visual Basic version 3.0, the width increases with each successive addition of text. In Visual Basic version 4.0, the width of the label increases once but does not continue to grow.

Example Workaround

To prevent the width from becoming excessively large, you can use this code:

   MsgBox "Start Width=" & Label1.Width
   OriginalWidth = Label1.Width
   Label1.Visible = False
   For i = 1 To 50
      Label1.Caption = Label1.Caption & "This is my Text "
   Next
   Label1.Width = OriginalWidth
   Label1.Visible = True
   MsgBox "Final Width=" & Label1.Width

This workaround suitable only for solving the large increases in label width that are seen in version 3.0. The AutoSize feature will probably cause the final width of the control to be slightly larger than the original width of the label as more text is added, even when the width is explicitly set. The differences between the initial and final widths of the control after text is added to the caption will depend on the content of the new caption and the font characteristics.

By setting the AutoSize property of the label to False before setting the label width back, the original width can be obtained. However, this will result in text being truncated.


Additional reference words: 4.00 3.00 Windows 95 vb4all
KBCategory: kbui kbusage
KBSubcategory: PrgCtrlsStd


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 19, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.