|
Subject:
|
I can't read 1 line at a time from a textbox
|
|
Posted By:
|
peterasimpson
|
Post Date:
|
8/14/2006 9:35:25 PM
|
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
|
|
Reply By:
|
Raghunathan
|
Reply Date:
|
8/15/2006 3:50:40 AM
|
Function to Get the specified Line from Multiline Text
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
CALLING THE FUNCTION
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
|
|
Reply By:
|
peterasimpson
|
Reply Date:
|
8/18/2006 2:33:26 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
|
|
Reply By:
|
BrianWren
|
Reply Date:
|
8/18/2006 11:45:45 AM
|
Try including all permutations of ways to make a line feed: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
CALLING THE FUNCTIONPrivate 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
|