Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
|
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 14th, 2006, 09:35 PM
Authorized User
 
Join Date: Nov 2005
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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

__________________
Simplicity is the key
 
Old August 15th, 2006, 03:50 AM
Authorized User
 
Join Date: Mar 2006
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to Raghunathan
Default

[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
 
Old August 18th, 2006, 02:33 AM
Authorized User
 
Join Date: Nov 2005
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old August 18th, 2006, 11:45 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to read a line from a text file ? nobitavn94 C# 3 November 25th, 2006 11:04 PM
How to read file line by line in EVC++ iriskab Visual C++ 0 September 27th, 2006 01:39 PM
How to read emails with certain subject line sketchit VB How-To 1 October 7th, 2005 09:07 AM
Read LF(Line Feed) at the end of the lines rylemer Pro VB 6 1 April 30th, 2004 02:16 AM





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