|
-
Jun 11th, 2003, 11:22 AM
#1
Thread Starter
Stuck in the 80s
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:
Option Explicit
'API Declarations:
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
'Constant Delcarations
Private Const EM_LINEINDEX = &HBB
Private Const EM_LINELENGTH = &HC1
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim lLen As Long
'Get the length of the current line:
lLen = SendMessage(Text1.hwnd, EM_LINELENGTH, EM_LINEINDEX, 0&)
'If length is 10, don't allow more characters:
If lLen = 10 And KeyAscii <> vbKeyReturn And KeyAscii <> vbKeyBack Then
KeyAscii = 0
End If
End Sub
-
Jun 12th, 2003, 10:45 AM
#2
Fanatic Member
or you can just do this:
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?
-
Jun 12th, 2003, 10:56 AM
#3
Originally posted by seec77
or you can just do this:
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
-
Jun 12th, 2003, 02:15 PM
#4
Thread Starter
Stuck in the 80s
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.
-
Jun 13th, 2003, 06:28 AM
#5
Fanatic Member
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?
-
Dec 30th, 2003, 01:36 PM
#6
Addicted Member
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:
Option Explicit
'API Declarations:
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
'Constant Delcarations
Private Const EM_LINEINDEX = &HBB
Private Const EM_LINELENGTH = &HC1
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim lLen As Long
'Get the length of the current line:
lLen = SendMessage(Text1.hwnd, EM_LINELENGTH, EM_LINEINDEX, 0&)
'If length is 10, don't allow more characters:
If lLen = 10 And KeyAscii <> vbKeyReturn And KeyAscii <> vbKeyBack Then
KeyAscii = 0
End If
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|