Using a TextArea, how do I limit the character length to 100 characters on each line upon entry even if pasted from the clipboard.
Sort of like the LimitText method but for each line.
thanks!
Using a TextArea, how do I limit the character length to 100 characters on each line upon entry even if pasted from the clipboard.
Sort of like the LimitText method but for each line.
thanks!
TextAreas have wordwrap (soft return), contrary to TextFields, so no line will display longer than the control anyway.
If you want lines to be cut with EndOfLine characters (hard return), you must program that yourself.
Sub TextChange()
dim linelength as integer = 100
dim ma(-1) as string
ma = split(me.text,EndOfLine)
for i as integer = 0 to ma.ubound
if len(ma(i))>linelength then
beep
dim mama as string = ma(i)
ma(i) =""
for ii as integer = 0 to len(mama)/linelength
ma(i) = ma(i)+"¶"+EndOfLine+mid(mama,ii*linelength,linelength)
next
end if
next
me.text = join(ma,EndOfLine)
End Sub
This will cut mechanically lines at the desired length set by linelength. I havae added the character ¶ to show hard return for debugging purposes.
Michel,
That worked pretty good for a single paste from clipboard. But if I type something or delete something in the textarea it has a strange behavior like creating a text line with one character, cutting off text, etc.
thanks for your help!
The issue is that in order to reformat, you got to mend lines, and truncate them once more.
To do that, you got to distinguish between the original paragraph end, and your imposed returns, in other words to make them kind of soft.
This can be achieved by a formatting routine that takes place not at every text change, but a short delay after it is idle. Here is an example I placed in a button action :
TextArea1.Text = replaceallb(TextArea1.Text,"¶"+chr(1)+chr(13),"")
dim linelength as integer = 100
dim ma(-1) as string
ma = split(TextArea1.text,EndOfLine)
for i as integer = 0 to ma.ubound
if len(ma(i))>linelength then
beep
dim mama as string = ma(i)
ma(i) =""
for ii as integer = 0 to len(mama)/linelength
ma(i) = ma(i)+"¶"+chr(1)+EndOfLine+mid(mama,ii*linelength,linelength)
next
end if
next
TextArea1.text = join(ma,EndOfLine)
You will notice that I am no longer simply adding ¶ and EndOfLine, but "¶"+chr(1)+EndOfLine
. That way I can distinguish between regular end of line, and “sort of hard” return. Chr(1) does not display, but is recognized by the program. As before, ¶ is here to see the effect of the method, and can be removed.
The first line uses chr(13) because inside the TextArea in Windows, that is the end of line character. In Mac, it is chr(10). That line removes the hard returns of a previous formatting, then the method cuts again 100 characters lines.
Now you got to figure where to put it so it takes place when needed.
Whew… Thanks for steering me in the right direction.
I was having problems until I used this
ma = ReplaceLineEndings(TextArea1.Text,EndOfLine).Split(EndOfLine)
In place of this:
ma = split(TextArea1.text,EndOfLine)
Now it works great, there’s only a slight bug left. It creates an extra blank line after correcting a line over 100 characters
Otherwise great!
Thanks for your help!