Link to home
Start Free TrialLog in
Avatar of eversoslightly
eversoslightly

asked on

Line by Line (not too hard)

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
ASKER CERTIFIED SOLUTION
Avatar of felonius
felonius

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AllenC_Jr
AllenC_Jr

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
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
Avatar of eversoslightly

ASKER

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.