HOWTO: Set the Formatting Rectangle of a Text Box

Last reviewed: May 23, 1997
Article ID: Q138798
The information in this article applies to:
  • Professional and Enterprise Editions of Microsoft Visual Basic, 16-bit only, for Windows, version 4.0

SUMMARY

This article demonstrates how to change the formatting rectangle of a text box to control when scrolling occurs.

The formatting rectangle determines the range of positions allowed for the caret (insertion point). The limiting rectangle is independent of the size of the edit-control window. By default, the formatting rectangle is the same as the client area of the edit-control window.

MORE INFORMATION

Using the SendMessage API call and the EM_SETRECT message, you can set the formatting rectangle of a text box. If you do not send the EM_SETRECT message, the formatting rectangle defaults to the size of the client area of the text box.

You can use this API call to control where the scrolling starts in a text box. The default scrolling starts when the insertion point reaches the left side of the text box. This API can make that rectangle smaller than the actual text box forcing the scrolling to start before the cursor reaches the left side of the text box.

NOTE: Lines of text exceeding the width of the text box (but smaller than the width of the formatting rectangle) are clipped instead of wrapped if all of the following conditions are true:

  • You do not use the message until after text has been entered into the text box.
  • The text box doesn't have a horizontal scroll bar.
  • The formatting rectangle is set to be larger than the text box window.

Step-by-Step Demonstration

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. Add a text box (Text1) to your form.

  3. Set the Text1 MultiLine Property to True and the ScrollBars Property to 3 (Both).

  4. Add a new module (Module1.bas) to the project.

  5. Add the following code to Module1.bas:

          Type recttype
    
             l As Integer ' left of rectangular region
             t As Integer ' top of region
             r As Integer ' right of region
             b As Integer ' bottom of region
          End Type
          ' Note the following Declare must be on one, single line:
          Declare Function SendMessage Lib "user" (ByVal hwnd%, ByVal wMsg%,
             ByVal wp%, lp As Any) As Long
    
    

  6. Add the following code to the Form_Load event for Form1:

          Private Sub Form_Load ()
    
             EM_SETRECT = &H403      ' Set EM_SETRECT variable
             Dim rect As recttype    ' dim variable as rectype
             rect.l = 0              ' Set left to upper left corner
             rect.t = 0              ' Set top to upper left corner
             rect.r = 200            ' Set right of region
             rect.b = 200            ' Set bottom of region
             x% = SendMessage(text1.hwnd, EM_SETRECT, 0, rect)
          End Sub
    
    

  7. Run the program.

Start typing in the text box. Scrolling will begin when you reach the edge of your region. You can change the size of your region by changing the values of the rect type.


Keywords : kbprg kbui PrgCtrlsStd kbhowto
Version : 4.0
Platform : WINDOWS
Issue type : kbhowto


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: May 23, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.