Getting each line from a text box (or RichTextBox) 
Author Message
 Getting each line from a text box (or RichTextBox)

Using VB5, can anyone please show me how to get each line from a
multi-line text box or RichTextBox?  So that I could have each line
stored in an array.

Regards, Otser.



Tue, 04 Dec 2001 03:00:00 GMT  
 Getting each line from a text box (or RichTextBox)

use this function where
    buffer = texbox.text ,
    delim = vbcrlf and
    stringarray = the array with the lines

Filip

------------------------------------------

Private Function ParseString(ByVal buffer As String, ByVal delim As String,
StringArray() As String) As Integer
    Dim count As Integer

    count = 0

    Do Until Len(buffer) = 0
        If InStr(buffer, delim) Then
            ReDim Preserve StringArray(count)
            StringArray(count) = Left(buffer, InStr(buffer, delim) - 1)
            buffer = Mid(buffer, InStr(buffer, delim) + 1)
            count = count + 1
        Else
            ReDim Preserve StringArray(count)
            StringArray(count) = buffer
            buffer = ""
            count = count + 1
        End If
    Loop

    ParseString = count
End Function

------------------------------------------


Quote:
> Using VB5, can anyone please show me how to get each line from a
> multi-line text box or RichTextBox?  So that I could have each line
> stored in an array.

> Regards, Otser.



Tue, 04 Dec 2001 03:00:00 GMT  
 Getting each line from a text box (or RichTextBox)

Quote:

>Using VB5, can anyone please show me how to get each line from a
>multi-line text box or RichTextBox?  So that I could have each line
>stored in an array.

Go to my web page and download PRNTXTBX.ZIP.

Lee Weiner
weiner AT fuse DOT net
http://home.fuse.net/lweiner



Tue, 04 Dec 2001 03:00:00 GMT  
 Getting each line from a text box (or RichTextBox)
Filip De Vos has already posted an answer which will enable you to get
complete "lines" (ie ending in a CrLf). Most people (and all standard word
processors) refer to these as paragraphs and so his code will get individual
paragraphs for you. If this is what you want then you can completely ignore
the rest of this message :-(

:-) You may, however, want to get lines exactly as they appear in the Text
Box. If this is what you want then the following stuff (which I got some
time ago from this Newsgroup, should do it for you.

Mike

Put the following in the General Declarations section of a form:
  Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    (ByVal hwnd As Long, ByVal wMsg As Long, _
    ByVal wParam As Long, lParam As Any) As Long
  Private Const EM_GETLINECOUNT = &HBA
  Private Const EM_GETLINE = &HC4
Use this code to get the lines:
  Dim lLines As Long, lRet As Long
  Dim i As Integer
  Dim sBuffer As String * 80
  'Make sure there's something in the box
  If Len(Text1) Then
    'Get the number of lines in the box
    lLines = SendMessage(Text1.hwnd, EM_GETLINECOUNT, 0, 0)
    'Set up the buffer to receive the text
    MsgBox "Number of Lines: " & lLines
    Mid$(sBuffer, 1, 1) = Chr$(79 And &HFF)
    Mid$(sBuffer, 2, 1) = Chr$(79 \ &H100)
    'Retrieve each line into the buffer & print
    For i = 0 To lLines - 1
      lRet = SendMessage(Text1.hwnd, EM_GETLINE, i, ByVal sBuffer)
      MsgBox Left$(sBuffer, lRet)
    Next
  End If
The code assumes there is no more than 79 characters in each line.  If you
have more, you need to increase the size of sBuffer and the numbers (79)
used
in setting up the buffer.


Quote:
> Using VB5, can anyone please show me how to get each line from a
> multi-line text box or RichTextBox?  So that I could have each line
> stored in an array.

> Regards, Otser.



Tue, 04 Dec 2001 03:00:00 GMT  
 Getting each line from a text box (or RichTextBox)
If using VB6, please lookup the Split() function: it automagically splits a
string at a specifiable delimiter and stores the splits in an array.


Tue, 04 Dec 2001 03:00:00 GMT  
 Getting each line from a text box (or RichTextBox)

Quote:
> use this function where
>     buffer = texbox.text ,
>     delim = vbcrlf and
>     stringarray = the array with the lines

> Filip

> ------------------------------------------

> Private Function ParseString(ByVal buffer As String, ByVal delim As
String,
> StringArray() As String) As Integer
>     Dim count As Integer

>     count = 0

>     Do Until Len(buffer) = 0
>         If InStr(buffer, delim) Then
>             ReDim Preserve StringArray(count)
>             StringArray(count) = Left(buffer, InStr(buffer, delim) - 1)
>             buffer = Mid(buffer, InStr(buffer, delim) + 1)
>             count = count + 1
>         Else
>             ReDim Preserve StringArray(count)
>             StringArray(count) = buffer
>             buffer = ""
>             count = count + 1
>         End If
>     Loop

>     ParseString = count
> End Function

The code above gave me an error message (can't remember what it said,
but it was to do with the "ReDim Preserve StringArray(count)" part.
Thanks anyway for trying to help, but I eventually worked out this
shorter solution:

Public r As Long, l As Long, tmp As String
Public line(0 to 999) As String

l = 0: r = 1: tmp = Text1

While r <> 0
    r = InStr(tmp, vbNewLine)
    If r <> 0 Then
        l = l + 1
        line(l) = Left(tmp, r - 1)
        tmp = Mid(tmp, r + 2)
    End If
Wend

Regards, Otser.



Wed, 05 Dec 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. getting a specific line of text from a text box (vb5)

2. Getting Line Text of a Rich Text Box - efficiently

3. Getting to a Line in a Rich Text Box

4. Printing other lines from Multi-Line text box

5. Line numbers into multi-line text box

6. ? Text Box: multi line; fixed length per line

7. Getting formatted text from Word into Richtextbox

8. Selecting a line of text from a RichTextBox

9. How to get centered text on a none multi line text box (VB4)

10. Highlighting lines of text in a rich text box

11. Assigning Multi line text to a multiline text box

12. Text Line In Text Box

 

 
Powered by phpBB® Forum Software