Results 1 to 9 of 9

Thread: Count Characters

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2007
    Location
    Merced
    Posts
    868

    Count Characters

    For a richtextbox how can I count all the characters on a current line?

  2. #2
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    804

    Re: Count Characters

    Code:
    Option Explicit
    Private Const EM_LINEINDEX = &HBB
    Private Const EM_LINELENGTH = &HC1
    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 Sub Form_Load()
     Dim s As String
     s = "First" & vbNewLine & "Second Line" & vbNewLine & _
       "A much longer line"
     Rich.Text = s
     Debug.Print LineLength(1) '==> 5
    End Sub
    
    Private Function LineLength(Line As Long) As Long
     'note Rich line numbers are zero based
     Dim FC As Long 'first character position of the specified line (also zero based)
     FC = SendMessage(Rich.hwnd, EM_LINEINDEX, Line - 1, ByVal 0&)
     LineLength = SendMessage(Rich.hwnd, EM_LINELENGTH, FC, ByVal 0&)
    End Function

  3. #3
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Count Characters

    Quote Originally Posted by GDOG34 View Post
    For a richtextbox how can I count all the characters on a current line?
    You didn't specify what you meant by "on a current line".
    This returns the char count (including spaces) of all the text. If you want just one line you're going to have to specify what constitutes a line, as in user selected text.


    Code:
    MsgBox Len(RichTextBox1.Text)
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  4. #4
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Count Characters

    Quote Originally Posted by VBClassicRocks View Post
    [CODE]
    Interesting code. I played with some additional code to loop through each line and return the chr count for each. Problem is it has a limit of 5 lines and it's not obvious to me where that limit is defined.

    Code:
    Private Const EM_LINEINDEX = &HBB   ' = 187
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  5. #5
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: Count Characters

    GDO, try like this
    Code:
    Dim lBegin As Long, lEnd As Long
    Dim CurPos As Long
    With RichTextBox1
        CurPos = .SelStart
        If CurPos = 0 Then CurPos = 1
        
        'Find line begin and end
        lBegin = InStrRev(.Text, vbCrLf, CurPos)
        lEnd = InStr(CurPos, .Text, vbCrLf)
        
        If lBegin = 0 Then lBegin = -1
        
        If lEnd = 0 Then
            lEnd = Len(.Text) - 1
        Else
            lEnd = lEnd - 2
        End If
    
        MsgBox "Chr Count of Current line " & lEnd - lBegin
    End With
    check and feedback.
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  6. #6
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Count Characters

    Quote Originally Posted by seenu_1st View Post
    GDO, try like this
    The line doesn't even need to be selected. Just have the cursor anywhere on that line... NICE!
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  7. #7
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Count Characters

    RTB's don't really have lines, they have paragraphs. A "line" is based on whether wrapping is on, which scrollbar settings you have, etc.

    What the heck do you really mean by "line" anyway? You can't look for CR or CRLF, since those are paragraph delimiters.

  8. #8
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: Count Characters

    You can't look for CR or CRLF, since those are paragraph delimiters.
    this is true.
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  9. #9
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: Count Characters

    hope this one works even if there is no CR or CRLF, try and feed back
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    Dim CurLine As Integer, Sline As Integer, Fline As Integer
    Dim CurPos As Integer, Spos As Integer, Epos As Integer
    Dim ChrCnt As Integer
    Dim Txt As String
    
    With RichTextBox1
        CurPos = .SelStart
        CurLine = .GetLineFromChar(CurPos)
        Spos = CurPos
        
        Do
            Spos = Spos - 1
            If Spos < 0 Then Exit Do 'in case of first line
            .SelStart = Spos
            Sline = .GetLineFromChar(Spos)
        Loop Until Not Sline = CurLine 'loop up to the line
        
        Spos = Spos + 1 'start position
        If Spos < 0 Then Spos = 0
        
        Epos = CurPos
        
        Do
            Epos = Epos + 1
            If Epos > Len(RichTextBox1) Then Exit Do 'in case of last line
            .SelStart = Epos
            Fline = .GetLineFromChar(Epos)
        Loop Until Not Fline = CurLine 'loop up to the line
        
        .SelStart = Spos
        .SelLength = Epos - Spos
        Txt = Replace(.SelText, vbCrLf, "") 'remove crlf if any
        ChrCnt = Len(Txt)
        .SelStart = CurPos
    End With
    MsgBox "Current line = " & Txt & vbNewLine & "Chr Count = " & ChrCnt
    End Sub
    edited: this line of code missed "CurPos = .SelStart" , now added
    Last edited by seenu_1st; Jul 22nd, 2011 at 10:55 PM. Reason: one line missed
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


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