-
Apr 9th, 2013, 05:48 PM
#1
Thread Starter
Member
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!
-
Apr 9th, 2013, 06:10 PM
#2
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!
-
Apr 9th, 2013, 07:56 PM
#3
Thread Starter
Member
Re: Help! Word counter for tabbed text editor?
Originally Posted by dunfiddlin
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?
-
Apr 9th, 2013, 08:02 PM
#4
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.
-
Apr 12th, 2013, 07:44 PM
#5
Thread Starter
Member
Re: Help! Word counter for tabbed text editor?
Originally Posted by Joacim Andersson
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.
-
Apr 13th, 2013, 07:09 PM
#6
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|