Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Line by Line (not too hard)

Posted on 1998-09-27
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

felonius earned 200 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 www.sand-stone.com


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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses

610 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