Line by Line (not too hard)

Posted on 1998-09-27
Last Modified: 2010-04-30
I use VB5

I have a richtextbox control...
in the box could be anything - for example:

whatever whatever
testing 123 123

Now - what I want to do is similar to the line input # command that you use with opening files.

I want to go similar like the line input command:
dim whatever as string
line input richtextbox1 whatever

Then, it would put "abcabc" in whatever
If I did it again, it would put "whatever whatever" in the box - doing each line each time.

Can you help me?  Thanks
Question by:eversoslightly
  • 2

Accepted Solution

felonius earned 50 total points
ID: 1436916
As far as I can see, what you want to do is read a line at the time from the textbox to a string. So what you need is a function which does this.

Is is ok to read past a wrapping line to the line ends? When reading from a file the line input # goes until it reaches end of file.

The following code can be run as is. Copy it into VB5. Make a Rich Text Box and a Label. Keep standard names. Run it. The program will read the lines one at the time and write them in the label in the opposite order. Viola!
It reads into the text property of RTB and when it encounters a newline it cuts it away and returns the line just read.
Call openrtf before each (re)start. You need more counters (an array maybe) if you want to read more boxes at the same time. If you change the contents of the box between read you must adjust the pointer variable accordingly.

So here goes:

Option Explicit

Dim pointer As Integer

Private Sub Form_Load()
  Dim s As String
  RichTextBox1.Text = "Hello" & vbCrLf & "123" & vbCrLf & "Test"
  Label1.Caption = ""
  s = inputrtf(RichTextBox1)
  While s <> "<End of Box>"
   Label1.Caption = s + vbCrLf + Label1.Caption
   s = inputrtf(RichTextBox1)
End Sub

' Call this before using input rtf. Requires pointer
' for each rtf box reading.
Sub openrtf()
  pointer = 1
End Sub

' Call this a get a string line back from the box
' Call openrtf before first call and each time you
' want to begin over.
Function inputrtf(a As RichTextBox) As String
  Dim i As Integer
  ' if we already read this string then exit with end of box
  If pointer > Len(a.Text) Then
    inputrtf = "<End of Box>"
    For i = pointer To Len(a.Text)
      ' If carriage return is read we have a line
      If Mid(a.Text, i, 2) = vbCrLf Then
        inputrtf = Mid(a.Text, pointer, i - pointer)
        pointer = i + 2
      Exit For
      End If
      ' the last line is also a line even without carriage return
      If i = Len(a.Text) Then
        inputrtf = Mid(a.Text, pointer, i - pointer + 1)
        pointer = i + 1
      End If
    Next i
  End If
End Function

Hope this helps. I gave a lot of time considering it being af 20 point question.


Expert Comment

ID: 1436917
That Code Is(no offense) way too complicated, there is a much easier way.

'Here is a MUCH EASIER WAY that allows the extraction of not only 'lines that are seperated with Carrage Return + Line Feed(vbCrLf) 'it allows you to seperate it with the characters you specify.
Dim Lines() as String
Dim Text as String
Text = "This is a test of the EBS|What Am I doing Here?|a|1"
GetLines Text, Lines(), "|"
Msgbox Lines(3)'Displays a Message Box With This text 'a'

'___     ___
'   \ | /
'    \|/
Public Function GetMatchCount&(Text, Search4)
    Dim cnt&
    For i = 1 To Len(Text)
        If Mid(Text, i, Len(Search4)) = Search4 Then
            cnt = cnt + 1
        End If
    GetMatchCount& = cnt
End Function

Public Function GetLines(Text, StrArray() As String, SplitChars As String)
    Dim Text2$, CLine$, CArrNum%
    CArrNum% = 1
    Text2 = Text
    If GetMatchCount(Text, SplitChars) = 0 Then
        Exit Function
    End If
      Redim StrArray(1 to GetMatchCount(Text, SplitChars) + 1)
    For i = 1 To GetMatchCount(Text, SplitChars) + 1
        If Not InStr(1, Text2, SplitChars) = 0 Then
            CLine$ = Mid(Text2, 1, InStr(1, Text2, SplitChars) - 1)
            CLine$ = Text2
        End If
        Text2 = Mid(Text2, InStr(1, Text2, SplitChars) + Len(SplitChars))
        StrArray(CArrNum) = CLine$
        CArrNum = CArrNum + 1
End Function

Expert Comment

ID: 1436918

How can something be way easier, when your code requires 28 lines of code compared to my 21 (both places not counting comments and the code to test the routines) and when yours use a ReDim command (slow) and furthermore uses much more memory. Yes, callerwise, it is easier with your solution as it only requires two calls to the routines, but:

1. You build an array in one shot. If using a rich text box that may get quite big and calling the function may take time noticibly slowing the application. The routine I proposed will get the lines one at the time. So the which is best is determined by its use. If you need to scan a line in a event handler and the text as been expanded since the last call to inputrtf your routine will fail.

2. Eversoslightly specifically asked for a routine with the supplied calling syntax. All_CJr does not do this.

And the delimiting character can also be another (or passed by parameter) in my routine. It just didn't do it here, as eversoslightly specificly asked for newlines. The extention can be done without adding new lines of code. If you take the delimiter as argument for inputrtf, you can change the delimiter for each string allowing for context dependant reads:
5   Hello
With af openrtf(),inputrtf(box,"   "),inputrtf(box,vbCrLF), I can make a primitive parser. Easy. For more advanced parsing try out Visual Parse ++, free for evaluation at


Author Comment

ID: 1436919
Thanks to both of you - both answers are correct - whichever way you look at it.

I'm giving you 50 points apiece.

Look for your points, AllenC_Jr, in another question I will post in a sec.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now