Results 1 to 7 of 7

Thread: Restrict the no. of characters in the Rich text box

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2005
    Posts
    2

    Restrict the no. of characters in the Rich text box

    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

  2. #2
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: Restrict the no. of characters in the Rich text box

    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)
    This world is not my home. I'm just passing through.

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2005
    Posts
    2

    Re: Restrict the no. of characters in the Rich text box

    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.


    Quote Originally Posted by trisuglow
    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)

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Restrict the no. of characters in the Rich text box

    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.

  5. #5
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: Restrict the no. of characters in the Rich text box

    how about using sendmessage something like this.
    VB Code:
    1. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    2. (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    3. Private Const EM_LINELENGTH = &HC1
    4.  
    5. Private Sub rtb_KeyPress(KeyAscii As Integer)
    6.  
    7. If SendMessage(rtb.hwnd, EM_LINELENGTH, rtb.SelStart, 0&) = 5 Then 'change the 5 to your number
    8.  rtb.Text = rtb.Text & vbCrLf
    9.  rtb.SelStart = Len(rtb.Text)
    10. End If
    11.  
    12. End Sub

    casey.

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Restrict the no. of characters in the Rich text box

    @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.

  7. #7
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: Restrict the no. of characters in the Rich text box

    Ahh !!, didnt think of that scenario. well spotted.

    casey.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width