Results 1 to 6 of 6

Thread: VB - Limit TextBox Line Length

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    VB - Limit TextBox Line Length

    Someone asked once if it would be possible to limit the length of each line in a text box to 10 characters. This is the code I came up with:

    VB Code:
    1. Option Explicit
    2.  
    3. 'API Declarations:
    4. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    5.   (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
    6.   lParam As Any) As Long
    7.  
    8. 'Constant Delcarations
    9. Private Const EM_LINEINDEX = &HBB
    10. Private Const EM_LINELENGTH = &HC1
    11.  
    12. Private Sub Text1_KeyPress(KeyAscii As Integer)
    13. Dim lLen As Long
    14.  
    15.   'Get the length of the current line:
    16.   lLen = SendMessage(Text1.hwnd, EM_LINELENGTH, EM_LINEINDEX, 0&)
    17.  
    18.   'If length is 10, don't allow more characters:
    19.   If lLen = 10 And KeyAscii <> vbKeyReturn And KeyAscii <> vbKeyBack Then
    20.       KeyAscii = 0
    21.   End If
    22.  
    23. End Sub
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    Fanatic Member seec77's Avatar
    Join Date
    Jan 2003
    Posts
    596
    or you can just do this:
    VB Code:
    1. Text1.MaxLen = 10
    a bit simpler
    Best Regards,
    seec77

    If you helped me, cosinder yourself thanked.

    Get each and every Garfield strip here!
    Here you can get all Calvin & Hobes strips!
    Damn UComics! It was probably unprofitable for them to allow us to just download Garfield and Calving & Hobes strips... so they made folder indexing unallowed on their server!!!

    I am 33% addicted to Counterstrike. What about you?
    I am 23% addicted to Star Wars. What about you?
    I am 0% addicted to Tupac. What about you?

  3. #3
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Originally posted by seec77
    or you can just do this:
    VB Code:
    1. Text1.MaxLen = 10
    a bit simpler
    Well, first, its MaxLength .

    Second, that will limit the size of the whole textbox. Hobo's way, I believe, works on a line by line basis.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  4. #4

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by crptcblade
    Second, that will limit the size of the whole textbox. Hobo's way, I believe, works on a line by line basis.
    Yes, indeedy.

    I don't know why you'd want such a thing, but I figured since one person asked, others may as well.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5
    Fanatic Member seec77's Avatar
    Join Date
    Jan 2003
    Posts
    596
    whoops...
    sorry, thought it was just for the whole textbox... anyway, sorry again...
    Best Regards,
    seec77

    If you helped me, cosinder yourself thanked.

    Get each and every Garfield strip here!
    Here you can get all Calvin & Hobes strips!
    Damn UComics! It was probably unprofitable for them to allow us to just download Garfield and Calving & Hobes strips... so they made folder indexing unallowed on their server!!!

    I am 33% addicted to Counterstrike. What about you?
    I am 23% addicted to Star Wars. What about you?
    I am 0% addicted to Tupac. What about you?

  6. #6
    Addicted Member
    Join Date
    Dec 2002
    Posts
    196

    Re: VB - Limit TextBox Line Length

    Great Code!

    One issue:

    I modified the code so that when the line reaches a certain length (10 for this example), text1.text = text1.text & VBCRLF so that the new text starts on the next line. If i then go back up to the previous line, I can type the 11th character and keep typing past the original 10 characters. Is there any way to prevent this?

    Originally posted by The Hobo
    Someone asked once if it would be possible to limit the length of each line in a text box to 10 characters. This is the code I came up with:

    VB Code:
    1. Option Explicit
    2.  
    3. 'API Declarations:
    4. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    5.   (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
    6.   lParam As Any) As Long
    7.  
    8. 'Constant Delcarations
    9. Private Const EM_LINEINDEX = &HBB
    10. Private Const EM_LINELENGTH = &HC1
    11.  
    12. Private Sub Text1_KeyPress(KeyAscii As Integer)
    13. Dim lLen As Long
    14.  
    15.   'Get the length of the current line:
    16.   lLen = SendMessage(Text1.hwnd, EM_LINELENGTH, EM_LINEINDEX, 0&)
    17.  
    18.   'If length is 10, don't allow more characters:
    19.   If lLen = 10 And KeyAscii <> vbKeyReturn And KeyAscii <> vbKeyBack Then
    20.       KeyAscii = 0
    21.   End If
    22.  
    23. End Sub
    Brandon S Davids

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