RichTextBox Control - reading each line of text and analyzing it

RichTextBox Control - reading each line of text and analyzing it

Post by Woof-Woo » Mon, 03 Nov 2003 03:32:50


Hello,

I have searched the net for a routine that will allow me to read 1
line of text at a time from a RichTextBox control and then do the same
for every line of text in the control. Each line in the control has a
fixed length and each line is the same length as all others. There may
be 5000 lines of text in the control and I have to be able to read
each one and then analyze it.

So far I'm going to brute force it by writing the contents of the
RichTextBox to a file using SaveFile, then opening the file using
Open, using Line Input #x and going from there. Is there a more
elegant and efficient way of accomphing the same thing?

Thank you.


---
Allen
 
 
 

RichTextBox Control - reading each line of text and analyzing it

Post by pietlinde » Mon, 03 Nov 2003 06:53:44


if each line of the richtextbox is of fixed length, can't you use
something really simple and stupid, like
Mid$(strRTBText,lngStart,cLINELENGTH) and then manipulate that?

 
 
 

RichTextBox Control - reading each line of text and analyzing it

Post by Woof-Woo » Mon, 03 Nov 2003 07:13:17

>> if each line of the richtextbox is of fixed length, can't you use
something really simple and stupid, like
Mid$(strRTBText,lngStart,cLINELENGTH) and then manipulate that?

I guess I don't understand how to find the start of each
line of text.

I guess I need to better understand the selRTF command.
I think what you are saying is save the whole file into one string
using
SelRTF, then parse the string according to the above.

But what if my line lengths were variable? There must be an easy
way to read each line (or go the the start of each line) in a
RichTextBox control and read until the end. That is what I'm afraid I
will need in the future.

Thank you.





same
a
may
if each line of the richtextbox is of fixed length, can't you use
something really simple and stupid, like
Mid$(strRTBText,lngStart,cLINELENGTH) and then manipulate that?

--


---
Allen
 
 
 

RichTextBox Control - reading each line of text and analyzing it

Post by mayayan » Mon, 03 Nov 2003 11:53:09


One easy way to do it would be to do a loop with Span:
(This assumes that each line has a VBCrLf)

Dim PT1 as long, Len1 as long
Dim s as string

Pt1 = 0
LenT = Len(RTB.Text)
Do
with RTB
.SelStart = PT1
.RTB.Span, vbCr, True, True '--select the line up to carriage
return.
Len1 = .SelLength '-- get length of line for next loop.
s = .SelText '--each time through loop s is the line.
PT1 = Len1 + 2 '--move ahead line length + len of vbcrlf
If (pt1 >= LenT) then exit do
End With
Loop

______________________________________________
Another way that's more involved but probably faster:
______________________________________________

Public Const EM_LINEINDEX = &HBB
Public Const EM_LINELENGTH = &HC1
Public Const EM_GETLINE = &HC4

Public Declare Function SendMessageLong Lib "user32" Alias "SendMessageA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam
As Long) As Long
Public Declare Function SendMessageString Lib "user32" Alias "SendMessageA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam
As String) As Long



Public Function RTBGetLine(RB As RichTextBox, LLineNum As Long) As String
'--get text of line.
'--get text of line. RB is RTB. LLineNum is line number.
Dim LineLen As Long, LIndex As Long
Dim sBuf As String
On Error GoTo woops
RTBGetLine = ""

'-- get line length from character, in this case index. Then
'-- put line length info. into first two bytes of GETLINE buffer.
'--
LIndex = SendMessageLong(RB.hwnd, EM_LINEINDEX, LLineNum, 0&)
LineLen = SendMessageLong(RB.hwnd, EM_LINELENGTH, LIndex, 0&) + 1
sBuf = String$((LineLen + 2), 0)
Mid$(sBuf, 1, 1) = Chr$(LineLen And &HFF)
Mid$(sBuf, 2, 1) = Chr$(LineLen \ &H100)

'--finally, call GETLINE:

LineLen = SendMessageString(RB.hwnd, EM_GETLINE, LLineNum, sBuf)
If (LineLen > 0) Then RTBGetLine = Left$(sBuf, LineLen)

Exit Function
woops:
End Function
 
 
 

RichTextBox Control - reading each line of text and analyzing it

Post by Rick Roths » Mon, 03 Nov 2003 11:57:10

> >> if each line of the richtextbox is of fixed length, can't you use

This is a general solution for the RichTextBox. Note that I use the Text
property and **not** the SelRTF.

Rick - MVP

Dim CurrentNewLine As Long
Dim NextNewLine As Long
Dim LineOfText As String
CurrentNewLine = -1
With RichTextBox1
NextNewLine = InStr(.Text, vbNewLine)
Do While Len(NextNewLine) > 0 And NextNewLine > 1
LineOfText = Mid$(.Text, CurrentNewLine + 2, NextNewLine -
CurrentNewLine - 2)
'
' Do whatever you want to this line
'
CurrentNewLine = NextNewLine
NextNewLine = InStr(CurrentNewLine + 2, .Text & vbNewLine, vbNewLine)
Loop
End With
 
 
 

RichTextBox Control - reading each line of text and analyzing it

Post by Rick Roths » Mon, 03 Nov 2003 12:42:38

Oops... line wrap. Here's the code with line continuation characters.

Rick - MVP

Dim CurrentNewLine As Long
Dim NextNewLine As Long
Dim LineOfText As String
CurrentNewLine = -1
With RichTextBox1
NextNewLine = InStr(.Text, vbNewLine)
Do While Len(NextNewLine) > 0 And NextNewLine > 1
LineOfText = Mid$(.Text, CurrentNewLine + 2, _
NextNewLine - CurrentNewLine - 2)
'
' Do whatever you want to this line
'
CurrentNewLine = NextNewLine
NextNewLine = InStr(CurrentNewLine + 2, _
.Text & vbNewLine, vbNewLine)
Loop
End With
 
 
 

RichTextBox Control - reading each line of text and analyzing it

Post by pietlinde » Mon, 03 Nov 2003 13:14:18

Okay, so you have a RichTextBox control and it has text in it. What
do you want to do with the text after you split it up? Just output it
to a text file? Cut to the chase. What are you going to do with the
text in the RTB? Once you explain that, you might get a more useful
answer.
 
 
 

RichTextBox Control - reading each line of text and analyzing it

Post by Woof-Woo » Tue, 04 Nov 2003 04:22:17

>> Okay, so you have a RichTextBox control and it has text in it.
What
do you want to do with the text after you split it up? Just output it
to a text file? Cut to the chase. What are you going to do with the
text in the RTB? Once you explain that, you might get a more useful
answer.

I want to be able to read each line of text in the RichTextBox and
stuff
each line into a string array.

As said, the approach I came up with to start was to dump the
RichTextBox
contents into a file, then use OPEN and LINE INPUT to read back the
contents
of the file and store the information into my array. I just figured
there was a more
direct way so I wouldn't need to save to file, then open the file and
read it a line
at a time - I figured why can't I read a line at a time directly from
the RichTextBox.

Thank you.



---
Allen
 
 
 

RichTextBox Control - reading each line of text and analyzing it

Post by Woof-Woo » Tue, 04 Nov 2003 04:25:07

Thank you. I will try all this a little later. Thanks for your help.



---
Allen
 
 
 

RichTextBox Control - reading each line of text and analyzing it

Post by Randy Birc » Tue, 04 Nov 2003 07:30:46

Is the line delimited by a hard return, or do you mean each physical line as
displayed, regardless as to whether there is a line termination character or
not? If you're looking for lines delimited by a hard return, and want those
into an array, just use Split ...

x() = split(RTF1.Text, vbcrlf)

If you need to use the physical non-delimitered line, you have to resort to
the API ... this demo, although for text boxes, works against the RTF
control as well ...
http://www.yqcomputer.com/ 'll need to
modify it to return a single line in each call.

--

Randy Birch
MVP Visual Basic
http://www.yqcomputer.com/
Please respond only to the newsgroups so all can benefit.




: >> Okay, so you have a RichTextBox control and it has text in it.
: What
: do you want to do with the text after you split it up? Just output it
: to a text file? Cut to the chase. What are you going to do with the
: text in the RTB? Once you explain that, you might get a more useful
: answer.
:
: I want to be able to read each line of text in the RichTextBox and
: stuff
: each line into a string array.
:
: As said, the approach I came up with to start was to dump the
: RichTextBox
: contents into a file, then use OPEN and LINE INPUT to read back the
: contents
: of the file and store the information into my array. I just figured
: there was a more
: direct way so I wouldn't need to save to file, then open the file and
: read it a line
: at a time - I figured why can't I read a line at a time directly from
: the RichTextBox.
:
: Thank you.
:
:
:
: ---
: Allen
:
:
 
 
 

RichTextBox Control - reading each line of text and analyzing it

Post by Woof-Woo » Tue, 04 Nov 2003 09:38:17

Yes, each line is delimited by a hard return.

I don't think split is a function in VB 5 - I am not using VB 6. I
suppose
I could write my own split routine.

Thanks, Randy.


physical line as
displayed, regardless as to whether there is a line termination
character or
not? If you're looking for lines delimited by a hard return, and want
those
into an array, just use Split ...

x() = split(RTF1.Text, vbcrlf)

resort to
the API ... this demo, although for text boxes, works against the RTF
control as well ...
http://www.yqcomputer.com/ 'll need
to
modify it to return a single line in each call.




---
Allen
 
 
 

RichTextBox Control - reading each line of text and analyzing it

Post by XXXXXXXXXX » Sat, 22 Nov 2003 20:01:35

When I use one I read into a string and use MID to go char by char