CodeItBetter Programming Another VB Programming Blog

How to wrap text to next line automatically in RichTextBox? (in two ways)

Posted on January 4, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
'Controls - How to wrap text to next line automatically in RichTextBox? (in two ways)
'Method 1:
'Set the RichTextBox's RightMargin property to the max line length that 
'you want this will start the text wrap once the line is the max length.

'Method 2 (Using API):
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, _
    ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const EM_LINELENGTH = &HC1
Private Const MaxLineLength = 20    'Change the maximum no. of characters per line as per your need

Private Sub RichTextBox1_Change()
    With RichTextBox1
        If SendMessage(.hWnd, EM_LINELENGTH, .SelStart, ByVal 0&) >= MaxLineLength Then
            .SelText = vbNewLine
        End If
    End With
End Sub
Filed under: Controls Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.