PRB: Label Grows Wider When AutoSize and WordWrap Are True

ID: Q135651

The information in this article applies to:

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: kbVBp400 kbVBp300 kbCtrl kbDSupport kbdsd

Version           : 3.0,4.0
Platform          : NT WINDOWS

Last Reviewed: August 24, 1998