Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 14th, 2006, 09:35 PM
Authorized User
 
Join Date: Nov 2005
Location: , , United Kingdom.
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
Reply With Quote
  #2 (permalink)  
Old August 15th, 2006, 03:50 AM
Authorized User
 
Join Date: Mar 2006
Location: Chennai, Tamil Nadu, India.
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
Reply With Quote
  #3 (permalink)  
Old August 18th, 2006, 02:33 AM
Authorized User
 
Join Date: Nov 2005
Location: , , United Kingdom.
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

Reply With Quote
  #4 (permalink)  
Old August 18th, 2006, 11:45 AM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 11:44 PM.


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