p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   VB How-To (http://p2p.wrox.com/forumdisplay.php?f=78)
-   -   I can't read 1 line at a time from a textbox (http://p2p.wrox.com/showthread.php?t=46514)

peterasimpson August 14th, 2006 09:35 PM

I can't read 1 line at a time from a textbox
 
Hello all,
I'm trying to read one line at a time from either a Multiline TextBox or Multiline RichTextBox.

Basically if I want to only read the 3rd or 7th line and not any other lines how can I do that. Any example code would be great.

Thanks
Pete


Raghunathan August 15th, 2006 03:50 AM

[u] Function to Get the specified Line from Multiline Text</u>

Public Function GetLine(ByVal MultiLineText As String, _
                Optional ByVal LineNo As Integer = 0) As String
If Trim(MultiLineText) = vbNullString Then Exit Function
Dim arrSentences() As String 'Creating a String Array
'Splitting the entire text into a array
'considering carriage return (enter key ==> vbCrLf) as the delimiter
arrSentences = Split(Trim(MultiLineText), vbCrLf)
GetLine = arrSentences(LineNo)
End If
End Function


[u]CALLING THE FUNCTION</u>

Private Sub Command1_Click()
MsgBox GetLine(Text1.Text, 3) 'To Retrieve the Third Line
MsgBox GetLine(Text1.Text, 7) 'To Retrieve the Seventh Line
MsgBox GetLine(Text1.Text) 'To Retrieve the First Line
End Sub


Regards,
Raghu

peterasimpson August 18th, 2006 02:33 AM

Thanks for the answer, that was really helpful and I learnt something.

The problem with this routine is that it relies on the VBCRLF(carriage return). What I'm after is a way to get a line and only that line if there are no VBCRLF(carriage return). For example if I were to copy an article from google news into a text box it does not realise that there is more that 1 line. only 'MsgBox GetLine(Text1.Text) 'To Retrieve the First Line' works even thought the Text1.Text might have 20 lines in it.

Thank you


BrianWren August 18th, 2006 11:45 AM

Try including all permutations of ways to make a line feed:
Code:

Public Function GetLine(ByVal MultiLineText As String, _
               Optional ByVal LineNo As Integer = 0 _
                       ) As String

    Dim arrSentences() As String ' Create a String Array

    If Trim(MultiLineText) = vbNullString Then Exit Function


    ' Normalize any alternates
    ' First make sure there is only one type of character
    MultiLineText = Replace(MultiLineText, VbCr, VbLf)

    ' Now you might have single instances of VbLf, or cases
    ' of VbLf & VbLf where you previously had VbCrLf.
    ' Eliminate duplicates.
    MultiLineText = Replace(MultiLineText, VbLf & VbLf, VbLf)

    ' Now do the split
    ' Split the entire text into a array
    ' considering Line Feeds as the delimiter
    arrSentences = Split(Trim(MultiLineText), vbLf)

    ' Now return the right element out of the array (zero-based)
    GetLine = arrSentences(LineNo)

End Function

[u]CALLING THE FUNCTION</u>
Code:

Private Sub Command1_Click()

    MsgBox GetLine(Text1.Text, 3) ' To Retrieve the 3rd Line
    MsgBox GetLine(Text1.Text, 7) ' To Retrieve the 7th Line
    MsgBox GetLine(Text1.Text)    ' To Retrieve the 1st (that is, the zeroeth) Line

End Sub



All times are GMT -4. The time now is 01:12 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.