BUG: Can't Set SelStart Property of RichTextBox to > 32K

Last reviewed: December 9, 1996
Article ID: Q160233
The information in this article applies to:
  • Standard, Professional, and Enterprise Editions of Microsoft Visual Basic, 32-bit only, for Windows, version 4.0

SYMPTOMS

You cannot set the SelStart property of RichTextBox controls that contain text greater than 32K (32768) characters in length to a value greater than 32767 and less than the text length. Although this is a legal range, SelStart is automatically set to the text length. Consequently, you cannot make selections within this range.

STATUS

Microsoft has confirmed this to be an issue in the Microsoft products listed at the beginning of this article. Microsoft is researching this issue and will post new information here in the Microsoft Knowledge Base as it becomes available.

WORKAROUND

Use the SendMessage API of USER32 with the EM_EXSETSEL parameter to set the SelStart position:

   Private Const WM_USER = &H400
   Private Const EM_EXSETSEL = (WM_USER + 55)

   Private Declare Function SendMessage Lib "USER32" Alias "SendMessageA"
   (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As
   Long) As Long

   Private Sub Command1_Click()
   Dim x As String

       x = String(32800, "a")
       RichTextBox1.Text = x

       '''[THE NORMAL WAY] RichTextBox1.SelStart = 32790
       SendMessage RichTextBox1.hwnd, EM_EXSETSEL, 0, 32790
       RichTextBox1.SelLength = 10

       MsgBox RichTextBox1.SelText

   End Sub

MORE INFORMATION

Steps to Reproduce Problem

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

  2. Place a RichTextBox, RichTextBox1, on Form1.

  3. Place a CommandButton, Command1, on Form1.

  4. Place the following code in the Click event of Command1:

       Private Sub Command1_Click()
       Dim x As String
    
           x = String(32800, "a")
           RichTextBox1.Text = x
    
           RichTextBox1.SelStart = 32790
           RichTextBox1.SelLength = 10
    
           MsgBox RichTextBox1.SelText
    
       End Sub
    
    

  5. Press the F5 key to run the project.

  6. Click Command1 and note that nothing is selected.


KBCategory: kbprg kbbuglist
KBSubcategory: PrgCtrls
Additional reference words: 4.00 kbstream vb4win vb432



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: December 9, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.