Results 1 to 6 of 6

Thread: Help! Word counter for tabbed text editor?

  1. #1

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Help! Word counter for tabbed text editor?

    I have this that I took from another forum post:
    Code:
    Dim tempArray()     As String
        Dim lngWordCount    As Long
        Dim lngCharCount    As Long
        Dim lngCharCountS    As Long
        Dim strTemp As String
        Dim intLen As Integer
        
        strTemp = RichTextBox1.Text
        Do Until Len(strTemp) = intLen
            intLen = Len(strTemp)
            strTemp = Replace(strTemp, "  ", " ")
        Loop
        tempArray = Split(Trim$(strTemp), " ")
        
        lngWordCount = UBound(tempArray) + 1    '~~~ Number of words. ie, delimited by a single space
        lngCharCount = Len(RichTextBox1.Text)   '~~~ Number of characters including spaces
        lngCharCountS = Len(Replace(RichTextBox1.Text, " ", "")) '~~~ Number of characters (excludes white spaces)
        
        '~~~ Display it
        MsgBox "Word Count = " & lngWordCount & vbNewLine & _
                "Character count(includes white spaces) = " & lngCharCount & vbNewLine & _
                "Character count(without white spaces) = " & lngCharCountS
    This works, just not for for a editor using a tabcontrol and a richtextbox.
    It would be great if you could help me someone!

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Help! Word counter for tabbed text editor?

    This works, just not for for a editor using a tabcontrol and a richtextbox.
    I don't see what difference a tabcontrol makes. It's designed to work with any RichTextBox so all you have to do is identify which RichTextBox it's working with.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Re: Help! Word counter for tabbed text editor?

    Quote Originally Posted by dunfiddlin View Post
    I don't see what difference a tabcontrol makes. It's designed to work with any RichTextBox so all you have to do is identify which RichTextBox it's working with.
    Thats the thing, I dont know how.
    Could you explain?

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Help! Word counter for tabbed text editor?

    If each TabPage you have in the TabControl only consist of one RichTextBox and nothing else then you only have to change this line:
    Code:
    strTemp = RichTextBox1.Text
    Into this:
    Code:
    strTemp = DirectCast(TabControl1.SelectedTab.Controls(0), RichTextBox).Text
    It casts the first control on the selected TabPage of the TabControl into a RichTextBox and returns its text.

  5. #5

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Re: Help! Word counter for tabbed text editor?

    Quote Originally Posted by Joacim Andersson View Post
    If each TabPage you have in the TabControl only consist of one RichTextBox and nothing else then you only have to change this line:
    Code:
    strTemp = RichTextBox1.Text
    Into this:
    Code:
    strTemp = DirectCast(TabControl1.SelectedTab.Controls(0), RichTextBox).Text
    It casts the first control on the selected TabPage of the TabControl into a RichTextBox and returns its text.
    The code you gave me works, but only when I use a button or anything similar.
    I tried using a timer to update the code every 1000 intervals but no luck there.
    More help would be appreciated....
    I need it so it updates as soon as a new word is typed.
    Right now my statuslabel has the same text it does before I start my program. I have this code setup:

    Code:
                Dim tempArray() As String
                Dim lngWordCount As Long
                Dim lngCharCount As Long
                Dim lngCharCountS As Long
                Dim strTemp As String
                Dim intLen As Integer
    
                strTemp = DirectCast(TabControl1.SelectedTab.Controls(0), RichTextBox).Text
                Do Until Len(strTemp) = intLen
                    intLen = Len(strTemp)
                    strTemp = Replace(strTemp, "  ", " ")
                Loop
                tempArray = Split(Trim$(strTemp), " ")
    
                lngWordCount = UBound(tempArray) + 1    '~~~ Number of words. ie, delimited by a single space
                lngCharCount = Len(RichTextBox1.Text)   '~~~ Number of characters including spaces
                lngCharCountS = Len(Replace(RichTextBox1.Text, " ", "")) '~~~ Number of characters (excludes white spaces)
    
                '~~~ Display it
                ToolStripStatusLabel4.Text = ("Word count = " & lngWordCount)
    
            End If
    Last edited by pancakesplatter; Apr 12th, 2013 at 07:48 PM.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,467

    Re: Help! Word counter for tabbed text editor?

    i'd use regex, + eventhandlers:

    Code:
    Imports System.Text.RegularExpressions
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            For Each tp As TabPage In TabControl1.TabPages
                AddHandler DirectCast(tp.Controls(0), RichTextBox).KeyPress, AddressOf RichTextBoxes_KeyPress
                AddHandler DirectCast(tp.Controls(0), RichTextBox).GotFocus, AddressOf RichTextBoxes_GotFocus
            Next
        End Sub
    
        Private Sub RichTextBoxes_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
            If e.KeyChar = " "c OrElse Char.IsControl(e.KeyChar) Then
                getWordCount(sender)
            End If
        End Sub
    
        Private Sub RichTextBoxes_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs)
            getWordCount(sender)
        End Sub
    
        Private Sub getWordCount(ByVal sender As Object)
            Dim rx As New Regex("\b[^\s.]+\b")
            Dim matches() As Match = rx.Matches(DirectCast(sender, RichTextBox).Text).Cast(Of Match).ToArray
            '~~~ Display it
            ToolStripStatusLabel4.Text = ("Word count = " & matches.Count)
        End Sub
    
    End Class

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