Computer Science Canada

Convert a MultiLine textbox Into strings for each line

Author:  GlobeTrotter [ Sat Apr 30, 2005 7:35 pm ]
Post subject:  Convert a MultiLine textbox Into strings for each line

I want to be able to have the user enter some data into a multiline textbox, and then seperate each line into a string, stored in an array.
This is what I have so far, but it doesn't really work.
VisualBASIC:

    Dim iCount As Integer
    Dim TempStr() As String
    Dim TempMarker As Integer
    Dim TempCount As Integer
    TempMarker = 1
    For iCount = 1 To Len(txtInput.Text) - 1
        If InStr(Mid(txtInput.Text, iCount, 2), vbNewLine) Then
            TempCount = TempCount + 1
            ReDim TempStr(TempCount)
            TempStr(TempCount) = Mid(txtInput.Text, TempMarker, iCount - TempMarker)
            TempMarker = iCount
        End If
    Next iCount

Author:  Brightguy [ Mon May 02, 2005 8:08 pm ]
Post subject:  Re: Convert a MultiLine textbox Into strings for each line

A couple little things:
-Use the Preserve keyword after ReDim to keep the data in the array when you resize it
-TempMarker represents the start of the next line, so it should be set to iCount + 2 (to skip the new line character)
-There isn't a new line character at the end of the text, so you'll have to add the last line into the array when the loop finishes
-You don't need to use the InStr function... simply check if Mid(txtInput.Text, iCount, 2) is the new line character

Author:  GlobeTrotter [ Mon May 02, 2005 8:48 pm ]
Post subject: 

Thanks a lot, I got it working. i have a question about it though...
code:
If Mid(txtInput.Text, iCount, 2) = vbNewLine Then

How does that line work? It's checking if a string of length 2 is equal to vbNewLine, which I assume is one character. How does that work?

Author:  Brightguy [ Thu May 05, 2005 9:22 am ]
Post subject:  Re: Convert a MultiLine textbox Into strings for each line

I normally use vbCrLf for new lines, which is two characters: carriage return + linefeed.

I thought vbNewLine was the same as vbCrLf, although after checking the VB help files, apparently vbNewLine is a platform-specific new line character, and sometimes doesn't include the linefeed character.


: