Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to count lines of text in a multi-line textbox and then incert into an array?

1,326 views
Skip to first unread message

Scott Laughon

unread,
Mar 28, 2001, 6:53:38 AM3/28/01
to
Hi there,
I have searched my books, the news groups and sites I know of and no
where can I find a good example of how to do this. What I am wanting to do
is to have a form with a multi-line textbox that I can past some text into
and have a command button. Set it up so that when I press the command
button the program counts the lines in the multi-line text box (going on the
return character) and then declares an array the correct size and inserts
the text line by line into the array so that I can then process it further.
I went through the help files in VB (I am using VB6) and found the following
code and I have followed all the instructions but it doesn't work. When I
press the CommandButton1 it throws an error saying "Compile error: Method or
data member not found". Then when I press OK to that it brings up the code
and the .LineCount section of the "Private Sub CommandButton1_Click()" sub
is highlighted. Any help would be much appreciated!!!

-------------------------------------------------------------
To use this example, copy this sample code to the Declarations portion of a
form. Make sure that the form contains the following controls:

A TextBox named TextBox1.
A CommandButton named CommandButton1.
Two Label controls named Label1 and Label2.
'Type SHIFT+ENTER to start a new line in the text box.


Private Sub CommandButton1_Click()
'Must first give TextBox1 the focus to get line count
TextBox1.SetFocus
Label1.Caption = "LineCount = " & TextBox1.LineCount
Label2.Caption = "TextLength = " & TextBox1.TextLength
End Sub

Private Sub UserForm_Initialize()
CommandButton1.WordWrap = True
CommandButton1.AutoSize = True
CommandButton1.Caption = "Get Counts"

Label1.Caption = "LineCount = "
Label2.Caption = "TextLength = "

TextBox1.MultiLine = True
TextBox1.WordWrap = True
TextBox1.Text = "Enter your text here."
End Sub
----------------------------------------------

Thank you for your time,

Scott Laughton.


wmf

unread,
Mar 28, 2001, 7:44:35 AM3/28/01
to
Private Sub CommandButton1_Click()
Dim lines
lines = Split(TextBox1.Text, vbCrLf)
Label1.Caption = "LineCount = " & UBound(lines)
Label2.Caption = "TextLength = " & Len(TextBox1.Text)
End Sub

Private Sub Form_Initialize()


CommandButton1.Caption = "Get Counts"
Label1.Caption = "LineCount = "
Label2.Caption = "TextLength = "

TextBox1.Text = "Enter your text here."
End Sub


"Scott Laughon" <sco...@sympac.com.au> wrote in message
news:99sj88$hv0$1...@perki.connect.com.au...

Phill W.

unread,
Mar 28, 2001, 7:56:17 AM3/28/01
to
Scott,

It's *extremely* cludgy, but you might be able to
work something using SendKeys.

(On the fly...)

Text1.SetFocus
SendKeys "^{HOME}", 1 ' Start of text
Do While Text1.SelStart < Len( Text1.Text)
' Select current line
SendKeys "+{END}", 1

sLine = .SelText

' Get to start of next line
SendKeys "{HOME}", 1
SendKeys "{DOWN}", 1
Loop

You'll probably have to experiment with the loop
condition ...

HTH,
Phill W.


Rick Rothstein

unread,
Mar 28, 2001, 8:07:57 AM3/28/01
to
Since the Variant array you called "lines" is a zero-based array,
UBound(lines) only gives the count of the number of newlines in the TextBox.
The count of the number of physical lines is one more than that. Also, it
would be better to explicitly convert the result of the UBound function to a
String before concatenating it to other text rather than relying on VB to do
it invisibly (which I don't think will be allowed in VB.NET). So

....
....
Label1.Caption = "LineCount = " & CStr(1 + UBound(lines))
Label2.Caption = "TextLength = " & CStr(Len(TextBox1.Text))
....
....

Rick


"wmf" <nospam-at-nospam.com> wrote in message
news:#wRgZT4tAHA.2248@tkmsftngp05...

Rocky Clark

unread,
Mar 28, 2001, 7:48:01 AM3/28/01
to
This will work for the carriage returns:

Dim sLines() As String

sLines = Split(Text1.Text, vbCrLf)

However, in a multiline TextBox, when there is a wordwrap at the edge of the
box, there is no carriage return unless the user hit enter. That's a little
more complicated.


"Scott Laughon" <sco...@sympac.com.au> wrote in message
news:99sj88$hv0$1...@perki.connect.com.au...

Mark Townshend

unread,
Mar 28, 2001, 8:54:49 AM3/28/01
to
Search the string for Returns CHR(13)
You could use the instr example on the MSDN help to loop through the whole
text.
e.g.

Function CountStrings(longstring As String)
Dim position, count
Dim aryStrings(0 To 20) As String

position = 1
startpos = 1
Do While InStr(position, longstring, Chr(13))
position = InStr(position, longstring, Chr(13)) + 2
aryStrings(count) = Mid(longstring, startpos, position - startpos -
2)
count = count + 1
startpos = position
Loop
aryStrings(count) = Right(longstring, Len(longstring) - position +
1)
For idx = 0 To count
Debug.Print "'" & aryStrings(idx) & "'"
Next idx
End Function

"Scott Laughon" <sco...@sympac.com.au> wrote in message
news:99sj88$hv0$1...@perki.connect.com.au...

MikeD

unread,
Mar 28, 2001, 9:13:01 AM3/28/01
to
The code example you found was for VBA in an MS Office application, not
Visual Basic.

In Visual Basic, the code won't work for 2 reasons: 1. A textbox does not
have WordWrap, LineCount, and TextLength properties (that's the cause of the
compile error). For that matter, a command button does not have AutoSize
and WordWrap properties. 2. The MultiLine property is read-only at
runtime. This would have caused a runtime error were it not for the compile
error.

The following KB article should be of assistance:

HOWTO: Print Multiline Text Box Using Windows API Functions
Article ID: Q140886
http://support.microsoft.com/support/kb/articles/Q140/8/86.asp

Mike


"Scott Laughon" <sco...@sympac.com.au> wrote in message
news:99sj88$hv0$1...@perki.connect.com.au...

Scott Laughton

unread,
Mar 29, 2001, 2:47:41 PM3/29/01
to
Thanks heaps Guys for all your very helpful replys!!!!!!

I have now got things happening!!!

Thank yuo all very much!!

Scott Laughton.

"Scott Laughon" <sco...@sympac.com.au> wrote in message
news:99sj88$hv0$1...@perki.connect.com.au...

0 new messages