Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting lines in textbox 8

Status
Not open for further replies.

Pete222

Programmer
Dec 29, 2002
85
0
0
GB
I want to click on cmd1 and label1's caption to say, as just a number, how many lines of text there are in text1's text.
Pete.
My site: clix.to/F
 
Assumming you are using a multiline textbox try something like this

Private Sub Command1_Click()
Dim arr() As String

arr() = Split(Text1.Text, vbCrLf)
Label1.Caption = UBound(arr)
End Sub

Hope I understood you correctly this time. [spin] If you choose to battle wits with the witless be prepared to lose.

[cheers]
 

Shouldn't that be...
[tt]
Label1.Caption = UBound(arr) +1
[/tt]
?

Good Luck


 

Based upon the defaults ... of which are not stated to be altered in this context.

 
I believe that Split will always return a zero-based array, regardless of option base setting!
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 

johnwm,

after testing the following code...
[tt]
Option Base 1
Option Explicit

Private Sub Form_Load()
Dim MyArray() As String, S As String
S = "A,B,C"
MyArray = Split(S, ",")

Debug.Print LBound(MyArray) & " to " & UBound(MyArray)

End Sub
[/tt]

Man, you are too good. You are right it seems to always return a zero based array.

Thanks (new trick/old dog)

 
Yes, VB's split function couldn't care less about Option Base; it always returns a zero-based array. Thus:

Label1.Caption = UBound(arr) + 1
 
But doesn't the UBound() function return the number of elements which is what we are looking for? If you are trying to loop through the elements to print then out it would matter but the original post just wanted the number of line (elements) so the array's base does not matter;

arr(0)
arr(1)
arr(2) 'Three elements

arr(1)
arr(2)
arr(3) 'Still three elements

?? [neutral] If you choose to battle wits with the witless be prepared to lose.

[cheers]
 

In my post with the code above praising johnwm the debug. print statement will print out...
[tt]
0 to 2
[/tt]
which will be the incorrect count of lines in a three line text box which is why I always knew that in scenarios like this I always needed to add 1 (+1) to get the correct line count (see post just after your first post). So in short UBound does not return the number of elements in an array it returns the upper limit of the array.

Good Luck

 
Ok, I follow you now. If you place my original post in an empty project does it work correctly? I am just wondering as it worked properly for me. If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
Never mind, a quit breakpoint shows that the Split function places an empty element on the end of the array so the UBound function returned the proper line count, strange luck for a quick post I guess. [flip] If you choose to battle wits with the witless be prepared to lose.

[cheers]
 

Yes it does because when you are entering information into the text property of the text box at design time it throws an extra vbcrlf in, but if you...
[tt]
Private Sub Command1_Click()
Text1.Text = ""
Text1.Text = "line1" & vbNewLine & "line2"

Dim arr() As String

arr() = Split(Text1.Text, vbCrLf)
Label1.Caption = UBound(arr)
End Sub
[/tt]
then it does not.

Good Luck


 
Correct. Sometimes it is better to be lucky rather than good! Point taken. [peace] If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
In an earlier post, CCLINT, gave his usually distinctive cleverly worded reply that split returns a zero based array no matter what you might be expecting or wanting. Robert Berman
Data Base consultant
Vulcan Software Services
thornmastr@yahoo.com
 
(which may involve less CCLINTs than you think it does)
 
I found nothing in that thread from CCLINT. Unfortunately, the Tek-Tip search engine is down. If time permits, I will try and find the thread I am referring to. Robert Berman
Data Base consultant
Vulcan Software Services
thornmastr@yahoo.com
 
I also made that point in this thread, 6th post down from top but I recall strongm making the same comment recently
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
To swith line in textbox Enter Key must be pressed. Based on that I wrote code
Code:
Option Explicit
Dim i As Long
Private Sub Command1_Click()
If Text1 <> &quot;&quot; Then
  i = 0
End If
Text2 = i
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then 'Enter was pressed
i = i + 1
End If
End Sub
This is not fully finished. Play with this and adjuste it according to youe needs.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top