Results 1 to 12 of 12

Thread: Numbered lines in a RichTextBox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    323
    How would I have each line numbered in a RichTextBox?
    If you think education is expensive, try ignorance.

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Try something like this:
    Code:
    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 Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As String) As Long
    Private Const EM_GETLINE = &HC4
    Private Const EM_GETLINECOUNT = &HBA
    Private Const EM_LINEINDEX = &HBB
    Private Const EM_LINELENGTH = &HC1
    
    Private Sub Command1_Click()
        Dim lngCount As Long
        Dim lngLineIndex As Long
        Dim lngLength As Long
        Dim strBuffer As String
        Dim strRichText As String
        Dim i As Integer
        
        'Get Line count
        lngCount = SendMessage(RichTextBox1.hwnd, EM_GETLINECOUNT, 0, 0)
        
        With RichTextBox1
            For i = 0 To lngCount - 1
                'Get line index
                lngLineIndex = SendMessage(.hwnd, EM_LINEINDEX, i, 0)
                'get line length
                lngLength = SendMessage(.hwnd, EM_LINELENGTH, lngLineIndex, 0)
                'resize buffer
                strBuffer = Space(lngLength)
                'get line text
                Call SendMessageStr(.hwnd, EM_GETLINE, i, ByVal strBuffer)
                'Number each line
                strRichText = strRichText & CStr(i + 1) & " " & strBuffer & vbCrLf
            Next
            'rewrite numbered text in RichTextBox
            .Text = strRichText
        End With
        
    End Sub
    
    Private Sub Form_Load()
        With RichTextBox1
            .Text = "line one" & vbCrLf
            .Text = .Text & "line two" & vbCrLf
            .Text = .Text & "line three"
        End With
    End Sub

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    323
    Thank you. I tried that and it is almost exactly what I'm looking for. I can work with that but what I really need is used sometimes in HTML editors like CuteHTML, it shows the line # for each line with text on it, but when you save the file it doesn't save the #s. Does that make sense to anyone? Sorry if I explained it wrong.
    If you think education is expensive, try ignorance.

  4. #4
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946
    Open "c:\afile.txt" For Output As #1
    Dim myline
    myline = richtextbox1.Text
    Write #1, myline
    Close

    produced this

    "1 line one
    2 line two
    3 line three
    "
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    323
    I'm sorry, but I don't understand that. I just put your code into my form under the

    Private Sub form_load()
    Open "c:\afile.txt" For Output As #1
    Dim myline
    myline = richtextbox1.Text
    Write #1, myline
    Close
    End Sub

    and it did nothing. Obviously a user error, but I don't understand what you are saying works?
    If you think education is expensive, try ignorance.

  6. #6
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    I think this is what you are looking for.

    I am assuming that the format for the textbox is

    #1 Line ones text
    #2 Line twos text

    so there is always a space after the line number.

    Code:
    Private Sub mySaveFile(strText As String)
        Dim ipCrlf1 As Long, ipCrLf2 As Long 'new line pointers
        Dim ipSp1 As Long  'spce pointer
            
        Dim strLine As String  'line of text
            
        Open "myFile.Txt" For Output As #1
            
        ipCrlf1 = 1: ipCrLf2 = 1
        'break up on line feeds
        Do
          ipSp1 = 1
          
          'find a line feed
          ipCrLf2 = InStr(ipCrlf1, strText, vbCrLf)
          If ipCrLf2 = 0 Then
            'if there isn't one then set the length to the string
            ipCrLf2 = Len(strText) + 1
          End If
          
          'get the line of text from the string
          strLine = Mid$(strText, ipCrlf1, ipCrLf2 - ipCrlf1)
          
          'find the first space character.
          ipSp1 = InStr(1, strLine, " ")
          If ipSp1 = 0 Then
            ipSp1 = 1
          Else
            ipSp1 = ipSp1 + 1
          End If
          
          'remove the line number
          strLine = Mid$(strLine, ipSp1, Len(strLine))
          
          ipCrlf1 = ipCrLf2 + 2
          
          Print #1, strLine
        Loop Until ipCrLf2 >= Len(strText)
        
        Close #1
        
    End Sub
    Hope it helps.
    Iain, thats with an i by the way!

  7. #7
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    ?

    ...that's cause I put it in the
    command button after the line numbers
    were added to the text box...
    in the form load you haven't added
    the numbers yet...

    yes it works,,,I tested it and wouldn't
    say it did if I didn't try it first...
    the output was just a cut and paste from
    the file I created and wrote to.

    Have fun....
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    323
    I don't want to sound stupid but I don't beleive I have any other options here.

    What is Private Sub mySaveFile(strText as String)?

    Is that a command button? I would have to have something named mySaveFile on my form for this to work right?

    I made a new form to try this. It has a command button which I named mySaveFile and a Rich Text Box called RichTextBox1. When I run it it does nothing. I know I'm doing something wrong, but I am very new to VB and could use any help you can offer. Thanks.
    If you think education is expensive, try ignorance.

  9. #9
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    as per your question

    no...it is a private sub that will save the file for you.

    if you start a new project and add a richtextbox1 and
    a command button (Command1) to it.
    then copy and paste the code Serge gave you into your applicaion. Here it is with the little piece I added to
    save your file to afile.txt on your c drive..

    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 Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As String) As Long
    Private Const EM_GETLINE = &HC4
    Private Const EM_GETLINECOUNT = &HBA
    Private Const EM_LINEINDEX = &HBB
    Private Const EM_LINELENGTH = &HC1

    Private Sub Command1_Click()
    Dim lngCount As Long
    Dim lngLineIndex As Long
    Dim lngLength As Long
    Dim strBuffer As String
    Dim strRichText As String
    Dim i As Integer

    'Get Line count
    lngCount = SendMessage(RichTextBox1.hwnd, EM_GETLINECOUNT, 0, 0)

    With RichTextBox1
    For i = 0 To lngCount - 1
    'Get line index
    lngLineIndex = SendMessage(.hwnd, EM_LINEINDEX, i, 0)
    'get line length
    lngLength = SendMessage(.hwnd, EM_LINELENGTH, lngLineIndex, 0)
    'resize buffer
    strBuffer = Space(lngLength)
    'get line text
    Call SendMessageStr(.hwnd, EM_GETLINE, i, ByVal strBuffer)
    'Number each line
    strRichText = strRichText & CStr(i + 1) & " " & strBuffer & vbCrLf
    Next
    'rewrite numbered text in RichTextBox
    .Text = strRichText
    End With

    End Sub

    Private Sub Form_Load()
    With RichTextBox1
    .Text = "line one" & vbCrLf
    .Text = .Text & "line two" & vbCrLf
    .Text = .Text & "line three"
    End With

    Open "c:\afile.txt" For Output As #1
    Dim myline
    myline = richtextbox1.Text
    Write #1, myline
    Close


    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  10. #10
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    again

    ..to use mysavefile sub do the same
    project with richtextbox1 and command1
    add all this code to the project and run it'adding line numbrs to the front of text in a rich text box
    '''you have to call the sub ...see the code in the
    end of command1 code

    lots of luck....
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'copy all this into your code section
    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 Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As String) As Long
    Private Const EM_GETLINE = &HC4
    Private Const EM_GETLINECOUNT = &HBA
    Private Const EM_LINEINDEX = &HBB
    Private Const EM_LINELENGTH = &HC1

    Private Sub Command1_Click()
    Dim lngCount As Long
    Dim lngLineIndex As Long
    Dim lngLength As Long
    Dim strBuffer As String
    Dim strRichText As String
    Dim i As Integer

    'Get Line count
    lngCount = SendMessage(richtextbox1.hwnd, EM_GETLINECOUNT, 0, 0)

    With richtextbox1
    For i = 0 To lngCount - 1
    'Get line index
    lngLineIndex = SendMessage(.hwnd, EM_LINEINDEX, i, 0)
    'get line length
    lngLength = SendMessage(.hwnd, EM_LINELENGTH, lngLineIndex, 0)
    'resize buffer
    strBuffer = Space(lngLength)
    'get line text
    Call SendMessageStr(.hwnd, EM_GETLINE, i, ByVal strBuffer)
    'Number each line
    strRichText = strRichText & CStr(i + 1) & " " & strBuffer & vbCrLf
    Next
    'rewrite numbered text in RichTextBox
    .Text = strRichText
    End With
    Open "c:\abcd.txt" For Output As #1
    Dim myline
    myline = richtextbox1.Text
    Write #1, myline
    Close
    End Sub

    Private Sub Form_Load()
    With richtextbox1
    .Text = "line one" & vbCrLf
    .Text = .Text & "line two" & vbCrLf
    .Text = .Text & "line three"
    End With

    Call mySaveFile(richtextbox1)
    End Sub
    '--------------------------------------------------------------------------------
    Private Sub mySaveFile(strText As String)
    Dim ipCrlf1 As Long, ipCrLf2 As Long 'new line pointers
    Dim ipSp1 As Long 'spce pointer

    Dim strLine As String 'line of text

    Open "myFile.Txt" For Output As #1

    ipCrlf1 = 1: ipCrLf2 = 1
    'break up on line feeds
    Do
    ipSp1 = 1

    'find a line feed
    ipCrLf2 = InStr(ipCrlf1, strText, vbCrLf)
    If ipCrLf2 = 0 Then
    'if there isn't one then set the length to the string
    ipCrLf2 = Len(strText) + 1
    End If

    'get the line of text from the string
    strLine = Mid$(strText, ipCrlf1, ipCrLf2 - ipCrlf1)

    'find the first space character.
    ipSp1 = InStr(1, strLine, " ")
    If ipSp1 = 0 Then
    ipSp1 = 1
    Else
    ipSp1 = ipSp1 + 1
    End If

    'remove the line number
    strLine = Mid$(strLine, ipSp1, Len(strLine))

    ipCrlf1 = ipCrLf2 + 2

    Print #1, strLine
    Loop Until ipCrLf2 >= Len(strText)

    Close #1

    End Sub



    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    323
    Thank you very much. I will work on that later on tonight, I'm sure it works though.
    If you think education is expensive, try ignorance.

  12. #12
    Hyperactive Member Zaphod64831's Avatar
    Join Date
    Mar 2000
    Posts
    268
    Instead of going through that long process of removing the lines and stuff, why not just make TWO RichTextBoxes, make one of them invisible and save your text to both. You can have the numbered one visible and save the invisible one's text.
    Email: [email protected]

    Home Page: www.olemac.net/~hutch

    I'm bored, VERY bored, and I got bored with my sig. So I changed it to this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width