Hi
I have a rich text box with multiline property set to yes. Now I want to restrict the max no. of characters that can be displayed in a line. what is the way ?
Shiva
Printable View
Hi
I have a rich text box with multiline property set to yes. Now I want to restrict the max no. of characters that can be displayed in a line. what is the way ?
Shiva
Is it acceptable to do this in the LostFocus event?
Do a loop through all the characters, counting as you go and resetting the count each time you come across a vbCr or vbLf (ASCII 13 and 10 respectively)
No it is not acceptable . if suppose i want to restrict the length to 25 cahracters and when the user enters the 26th character it has to automatically go to the next line or if a user deletes a charcter it has to rearrange. :confused:
Quote:
Originally Posted by trisuglow
You can set the RightMargin property to force the RichTextBox to wrap the lines at this position. However the scale used for this property depends on the ScaleMode of the Form the RTB resides on and unless you use a mono-spaced font you can not set this to a certain amount of characters.
how about using sendmessage something like this.
VB Code:
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 Sub rtb_KeyPress(KeyAscii As Integer) If SendMessage(rtb.hwnd, EM_LINELENGTH, rtb.SelStart, 0&) = 5 Then 'change the 5 to your number rtb.Text = rtb.Text & vbCrLf rtb.SelStart = Len(rtb.Text) End If End Sub
casey.
@Casey: I was also thinking about the EM_LINELENGTH message but it won't work in the manner you have presented it with. In your code it will work if you're only adding text to the end of a document but not anywhere else. Let's say you already have 10 lines of text and you want to insert text on line 5. When that reach the limit of however many characters you want to use on a line your code will add a linebreak at end of the document and then jump to that position.
Ahh !!, didnt think of that scenario. well spotted.
casey.