Results 1 to 9 of 9

Thread: Richtextbox - Display Line Number

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Posts
    127
    Dear all...

    I have a richtext box, and as you know there is a line property, we can say go to line 1, or etc... my question is how do i make that line number appear. Make the line numbers visible.
    ASPWIZARD

    http://www.aspwizard.co.uk

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Here's is a work around:
    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_GETFIRSTVISIBLELINE = &HCE
    Private Const EM_GETLINE = &HC4
    Private Const EM_LINEINDEX = &HBB
    Private Const EM_LINELENGTH = &HC1
    
    
    
    Public Sub SetTopLineVisible(p_intLineNumber As Integer, p_Rich As RichTextBox)
        Dim lngRetVal As Long
        Dim lngLineIndex As Integer
        Dim lngLength As Integer
        Dim strBuffer As String
        Dim lngPos As Long
            
        With p_Rich
            'Get line index
            lngLineIndex = SendMessage(.hwnd, EM_LINEINDEX, p_intLineNumber, 0)
            'get line length
            lngLength = SendMessage(.hwnd, EM_LINELENGTH, lngLineIndex, 0)
            'resize buffer
            strBuffer = Space(lngLength)
            'get line text
            Call SendMessageStr(.hwnd, EM_GETLINE, p_intLineNumber, ByVal strBuffer)
            'Find first character postion
            lngPos = InStr(.Text, strBuffer)
            'Set SelStart to the found character index
            .SelStart = lngPos
        End With
    End Sub
    Then call this routine like this:

    SetTopLineVisible 5, RichTextBox1

    [Edited by Serge on 10-20-2000 at 12:20 PM]

  3. #3
    Guest
    Your code doesn't seem to work for me Serge.

    jjferreira, I thought SendKeys would do it, so I wrote the code out..had a few things wrong, but I still knew SendKeys would do it, and it does, I finally got it .


    Code:
    Private Sub Command1_Click()
        If Not RichTextBox1.Text = "" Then
            RichTextBox1.SelStart = 0
        For i = -0 To RichTextBox1.GetLineFromChar(RichTextBox1.SelStart) + RichTextBox1.GetLineFromChar(Len(RichTextBox1.Text))
            RichTextBox1.SetFocus
            SendKeys "{HOME}"
            SendKeys i & ". "
            SendKeys "{DOWN}"
            SendKeys "{HOME}"
        Next i
        End If
    End Sub

  4. #4
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Matthew, I don't understand why the code wouldn't work for you. Are you special??? (lol)

  5. #5
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 2000
    Location
    Minnesota
    Posts
    666
    I believe he want's to show the line Numbers, not jump to a specific line!!!

    Will try to draw up some code and post it. Maybe in a while
    GWDASH
    [b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]

  6. #6
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Then 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

  7. #7
    Guest
    That one works Serge. Much more advanced code than the sample I gave , even though both of 'em work.

    Does the first code work for you? Try it! I opened VB6 and put the code from your first post into the Form, and put the SetTopLineVisible 5, RichTextBox1 in a Command Button and it don't work. But the code above works fine. Weird, aye?

    But see, what if you didn't have an answer! Than my code would've come into use! But...nope, just another code.

    Maybe I'm special. Can someone else try the code form Serge's first post and tell me if it works. Than we can determine the analysis (*tries to sound smart*) to solve the equation to see if I am really special. Or maybe I might just be another person in the crowd.

  8. #8
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 2000
    Location
    Minnesota
    Posts
    666
    Didn't work for me either

    I broke it down, an setting the SelStart doesn't scroll, it just puts the cursor in that pos.(at least for me). Plus, the cursor position was off by about 1.5 lines
    GWDASH
    [b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]

  9. #9
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    I found what the problem was. So here is a new approach to achieve 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 Const EM_GETFIRSTVISIBLELINE = &HCE
    Private Const EM_LINESCROLL = &HB6
    
    Public Sub SetTopLineVisible(p_RichText As Control, p_lngLineNumber As Long)
        Dim lngCurTopLine As Long
        Dim lngLinesToScroll As Long
        
        lngCurTopLine = SendMessage(p_RichText.hwnd, EM_GETFIRSTVISIBLELINE, 0, 0)
        
        lngLinesToScroll = p_lngLineNumber - lngCurTopLine
        
        Call SendMessage(p_RichText.hwnd, EM_LINESCROLL, 0&, ByVal lngLinesToScroll)
    End Sub
    Then call this routinge similar way as the privious one.

    Call SetTopLineVisible(RichText1, 5)

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