|
-
Mar 26th, 2003, 02:31 PM
#1
Thread Starter
Fanatic Member
MultiLine TextBox [RESOLVED]
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
Last edited by JCScoobyRS; Mar 26th, 2003 at 03:16 PM.
He who listens well, speaks well.
-
Mar 26th, 2003, 02:37 PM
#2
Fanatic Member
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.
there are 2 reasons why i leave my work unfinished:
(1) i'm getting old.
-
Mar 26th, 2003, 02:39 PM
#3
Frenzied Member
Originally posted by radum
mltiline textboxes automatically wrap the text so that it doesnt go out to the right
...Unless you set the scrollbar property, that is!
-
Mar 26th, 2003, 02:44 PM
#4
Stuck in the 80s
Re: MultiLine TextBox
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
This code will do what you asked:
VB Code:
Option Explicit
'Code by Kristopher Wilson
'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
-
Mar 26th, 2003, 02:49 PM
#5
Thread Starter
Fanatic Member
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
He who listens well, speaks well.
-
Mar 26th, 2003, 02:56 PM
#6
Frenzied Member
Worked perfectly for me with no modifications, in the Form's code window. What error did you get?
-
Mar 26th, 2003, 03:05 PM
#7
Stuck in the 80s
Re: Re: MultiLine TextBox
VB Code:
Option Explicit
'Code by Kristopher Wilson
'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.
-
Mar 26th, 2003, 03:07 PM
#8
Thread Starter
Fanatic Member
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
He who listens well, speaks well.
-
Mar 26th, 2003, 03:08 PM
#9
Thread Starter
Fanatic Member
DUH!!! Hobo, that fixed it. Let's give it a try and I'll be back with the results. Thanks, Jeremy
He who listens well, speaks well.
-
Mar 26th, 2003, 03:10 PM
#10
Stuck in the 80s
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.
-
Mar 26th, 2003, 03:15 PM
#11
Thread Starter
Fanatic Member
Very Nice...it works. I didn't doubt it but I am very impressed. I need to learn that kind of stuff. Thanks, Jeremy
He who listens well, speaks well.
-
Mar 26th, 2003, 03:18 PM
#12
Thread Starter
Fanatic Member
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
He who listens well, speaks well.
-
Mar 26th, 2003, 03:31 PM
#13
Stuck in the 80s
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:
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.
-
Mar 26th, 2003, 03:34 PM
#14
-
Mar 26th, 2003, 03:50 PM
#15
Thread Starter
Fanatic Member
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
He who listens well, speaks well.
-
Mar 26th, 2003, 03:56 PM
#16
Stuck in the 80s
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:
-
Mar 26th, 2003, 05:00 PM
#17
Thread Starter
Fanatic Member
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
He who listens well, speaks well.
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
|