I have a TextBox that allows multiple lines. I need to be able to limit each line to only 30 characters. How can I achieve this? Thanks, Jeremy
Printable View
I have a TextBox that allows multiple lines. I need to be able to limit each line to only 30 characters. How can I achieve this? Thanks, Jeremy
mltiline textboxes automatically wrap the text so that it doesnt go out to the right. if you use a fixed char width font in your text box you can size the textbox so that it only shows 30 chars per line. but i doubt this is a decent solution for you... other than that, you will have to insert vbCrLf characters every 30 chars. you can do this inside the _Change() event of your text box. first solution will keep your text free of CRLF sequences, 2nd solution will change the user input.
...Unless you set the scrollbar property, that is!Quote:
Originally posted by radum
mltiline textboxes automatically wrap the text so that it doesnt go out to the right
This code will do what you asked:Quote:
Originally posted by JCScoobyRS
I have a TextBox that allows multiple lines. I need to be able to limit each line to only 30 characters. How can I achieve this? Thanks, Jeremy
VB Code:
Option Explicit 'Code by Kristopher Wilson 'Email: [email][email protected][/email] 'Web: [url]http://www.vbshelf.com[/url] 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_LINEINDEX = &HBB Private Const EM_LINELENGTH = &HC1 Private Sub Text1_KeyPress(KeyAscii As Integer) Dim lLen As Long lLen = SendMessage(Text1.hwnd, EM_LINELENGTH, EM_LINEINDEX, 0&) If lLen = 30 And KeyAscii <> vbKeyReturn Then KeyAscii = 0 End Sub
This may sound dumb but how do I use your code? I have tried to copy and paste into my Form's Source but I got an error. Does this have to be in a module? Thanks, Jeremy
Worked perfectly for me with no modifications, in the Form's code window. What error did you get?
VB Code:
Option Explicit 'Code by Kristopher Wilson 'Email: [email][email protected][/email] 'Web: [url]http://www.vbshelf.com[/url] 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_LINEINDEX = &HBB Private Const EM_LINELENGTH = &HC1
Put the above portion at the very top of the form code window. If you already have some declarations there, just fit it in accordingly.
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer) Dim lLen As Long lLen = SendMessage(Text1.hwnd, EM_LINELENGTH, EM_LINEINDEX, 0&) If lLen = 30 And KeyAscii <> vbKeyReturn Then KeyAscii = 0 End Sub
Put the above portion in with the rest of your code and change all occurances of Text1 to the name of your TextBox.
VB Code:
Compile Error: Only comments may appear after End Sub, End Function or End Property
This code is what's highlighted:
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
Probably something easy, I just have been working on this diligently and I overlook somethings. Thanks, Jeremy
DUH!!! Hobo, that fixed it. Let's give it a try and I'll be back with the results. Thanks, Jeremy
You might want to update the code to this:
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer) Dim lLen As Long lLen = SendMessage(Text1.hwnd, EM_LINELENGTH, EM_LINEINDEX, 0&) If lLen = 10 And KeyAscii <> vbKeyReturn And KeyAscii <> vbKeyBack Then KeyAscii = 0 End Sub
This makes it so backspace will work when there's 10 characters.
Very Nice...it works. I didn't doubt it but I am very impressed. I need to learn that kind of stuff. Thanks, Jeremy
Now, the next task. Is there a way to get the value, as a string, from each line where each line is a new value? Or is there a way to reference the line through code:
txtVariable = TextBox.Line1.Text
Thanks, Jeremy
This would be the easiest way:
VB Code:
Private Function GetLine(txtBox As TextBox, iLine As Integer) Dim strLines() As String strLines() = Split(txtBox.Text, vbCrLf) GetLine = strLines(iLine - 1) End Function
To use it, just do:
VB Code:
Msgbox GetLine(Text1, 2)
Where Text1 is the name of the box, and 2 is the line number you want. Note: you could assign this to a variable instead of MsgBoxing it.
Check out the attachment. It does what you want and it has some other nice features.
Ok...let's say that for each line in the TextBox, I have to do an insert into a database. How should I go about doing this? Thanks, Jeremy
Something like:
VB Code:
Private Sub DoInsert(txtBox As TextBox) Dim strLines() As String, i As Integer strLines() = Split(txtBox.Text, vbCrLf) For i = 0 To Ubound(strLines) 'code to insert line here 'line can be accessed by strLines(i) Next End Function
Then just call the subroutine when you need it:
VB Code:
DoInsert Text1
That works...what about pulling data from a database. If I retrieve 3 rows, I need to have 3 lines in the TextBox with the corresponding Text in that row. Can you help, again? Thanks for your continued support and help, Jeremy