954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Reading line breaks in a textbox

Hey i have a textbox that i want to read line by line and store in an ArrayList. I am currently trying to find the line break at the end of each line. Here is the code:

While i = 0
            h = ptext.IndexOf(vbCrLf)'here is where i try to find line break. i have tried everything from Chr(10) to vbLf
            If h < 0 Then
                h = ptext.Length
                i = 1
            End If
            a.Add(ptext.Substring(0, h))
            ln += 1
            If i = 0 Then ptext = ptext.Substring(h, ptext.Length)
            h = -7
        End While

Any suggestions?
thanks
m

nschessnerd
Posting Whiz in Training
216 posts since Dec 2006
Reputation Points: 10
Solved Threads: 8
 

Hi,

Use "Split" Function.

Dim MyArr
  Dim i As Long
  MyArr = Split(TextBox1.Text, VbCrLf)
  For i = 0  To UBound(MyArr)
      Debug.Print MyArr(i)
  Next

Regards
Veena

QVeen72
Posting Shark
950 posts since Nov 2006
Reputation Points: 84
Solved Threads: 143
 

Hi, Use "Split" Function.

Dim MyArr
  Dim i As Long
  MyArr = Split(TextBox1.Text, VbCrLf)
  For i = 0  To UBound(MyArr)
      Debug.Print MyArr(i)
  Next

Regards Veena




A better way is as follows

If InStr(ptext,chr(10))> 0 Then
MsgBox " ptext contains chr10"
endif

regards
AV Manoharan

AV Manoharan
Junior Poster
166 posts since Jun 2007
Reputation Points: 10
Solved Threads: 9
 

Hi Manoharan,

What if there are "Multiple Line Breaks" in textbox....?
Put in a loop..? Why do the excersise when it can be done in single statement.

Regards
Veena

QVeen72
Posting Shark
950 posts since Nov 2006
Reputation Points: 84
Solved Threads: 143
 

Hi,

Use "Split" Function.

Dim MyArr
  Dim i As Long
  MyArr = Split(TextBox1.Text, VbCrLf)
  For i = 0  To UBound(MyArr)
      Debug.Print MyArr(i)
  Next

Regards Veena


wow thanks, worked great!

nschessnerd
Posting Whiz in Training
216 posts since Dec 2006
Reputation Points: 10
Solved Threads: 8
 

The only problem is the MyArr declaration don't declare it like that (variant)

~Paul~
Light Poster
31 posts since May 2007
Reputation Points: 35
Solved Threads: 0
 
The only problem is the MyArr declaration don't declare it like that (variant)


yea instead of having debug print it, i added each one to an arraylist

nschessnerd
Posting Whiz in Training
216 posts since Dec 2006
Reputation Points: 10
Solved Threads: 8
 
Hi Manoharan, What if there are "Multiple Line Breaks" in textbox....? Put in a loop..? Why do the excersise when it can be done in single statement. Regards Veena



Veena, I think he is not using the textbox as a pipe or stream, so that they can contain any nubmebr of Carriage Returnes and Line Feeds. In a line of Text there can never be two such things.

Eventhough your programming code takes some overheads, it worked for him.

Thanks

AV Manoharan

AV Manoharan
Junior Poster
166 posts since Jun 2007
Reputation Points: 10
Solved Threads: 9
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You