Solved

Line by Line (not too hard)

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

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

abcabc
whatever whatever
testing 123 123
hellooooo

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
0
Comment
Question by:eversoslightly
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
felonius earned 50 total points
Comment Utility
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"
  openrtf
  Label1.Caption = ""
  s = inputrtf(RichTextBox1)
  While s <> "<End of Box>"
   Label1.Caption = s + vbCrLf + Label1.Caption
   s = inputrtf(RichTextBox1)
  Wend
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>"
  Else
    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.

felonius
0
 
LVL 2

Expert Comment

by:AllenC_Jr
Comment Utility
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.
'Example:
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
    Next
    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)
        Else
            CLine$ = Text2
        End If
        Text2 = Mid(Text2, InStr(1, Text2, SplitChars) + Len(SplitChars))
        StrArray(CArrNum) = CLine$
        CArrNum = CArrNum + 1
    Next
End Function
0
 
LVL 1

Expert Comment

by:felonius
Comment Utility
Allen_CJr,

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

felonius
0
 
LVL 1

Author Comment

by:eversoslightly
Comment Utility
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.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

771 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

11 Experts available now in Live!

Get 1:1 Help Now