|
-
Jan 17th, 2012, 09:57 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Word Count
Hi All
I have this code:
Code:
Private Sub tbUpperBody_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbUpperBody.TextChanged
Dim c As String() = tbUpperBody.Text.Split(" ")
Dim wc As Integer = 0
For i As Integer = 0 To c.Count - 1
If Trim(c(i)) <> "" Then wc += 1 'ignore items in array that have only spaces
Next
ToolStripLabel1.Text = "[TL: " & Math.Round(wc / 3, 0) & "]" 'total word count
If (wc / 3) >= 60 Then
ToolStripLabel1.Text = "[TL: " & Math.Round((wc / 3) / 60, 2) & "]"
End If
End Sub
which counts the words in the text box tbUpperBody. It then divides the number of words by 3. I need the value of ToolStripLabel1 to be a time. So, this means that if the value of the word count after it's divided by 3 is greater than 60, it needs to display it as 01:23 (for one minute, 23 seconds)
I cannot get the label to format the number correctly. It gives me values in decimals at the moment, for example "1.78" which makes no sense int he context of a time value.
I really appreciate any help on this one!
Thanks
-
Jan 17th, 2012, 10:12 AM
#2
Addicted Member
Re: Word Count
You need to construc a timespan, using total seconds...
 Originally Posted by MacShand
Hi All
I have this code:
Code:
Private Sub tbUpperBody_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbUpperBody.TextChanged
Dim c As String() = tbUpperBody.Text.Split(" ")
Dim wc As Integer = 0
For i As Integer = 0 To c.Count - 1
If Trim(c(i)) <> "" Then wc += 1 'ignore items in array that have only spaces
Next
ToolStripLabel1.Text = "[TL: " & Math.Round(wc / 3, 0) & "]" 'total word count
If (wc / 3) >= 60 Then
ToolStripLabel1.Text = "[TL: " & Math.Round((wc / 3) / 60, 2) & "]"
End If
End Sub
which counts the words in the text box tbUpperBody. It then divides the number of words by 3. I need the value of ToolStripLabel1 to be a time. So, this means that if the value of the word count after it's divided by 3 is greater than 60, it needs to display it as 01:23 (for one minute, 23 seconds)
I cannot get the label to format the number correctly. It gives me values in decimals at the moment, for example "1.78" which makes no sense int he context of a time value.
I really appreciate any help on this one!
Thanks
-
Jan 17th, 2012, 10:15 AM
#3
Thread Starter
Addicted Member
Re: Word Count
Hi Lectere, thanks for the reply.
I'm very new to programming and so am not sure how to go about that. can you advise?
many thanks.
-
Jan 17th, 2012, 10:16 AM
#4
Addicted Member
Re: Word Count
To chew it out even more for you;
Code:
MsgBox((New TimeSpan(0, 0, 630)).ToString)
 Originally Posted by MacShand
Hi Lectere, thanks for the reply.
I'm very new to programming and so am not sure how to go about that. can you advise?
many thanks.
-
Jan 17th, 2012, 10:45 AM
#5
Hyperactive Member
Re: Word Count
You're already making use of Math.Round which will round the number up or down thus meaning it's not possible for you to be left with 1.78.
A problem with your programming logic is the effect that the time going over 1 minute would have upon the output.
Probably easiest as an example:
wc = 192
(192/3) = 64 Which is in fact larger or equal to 60.
64/60 = ~1.06666666667
Round that and you'll be left with 1 minute 0 Seconds rather than 1 minute 4 seconds.
To counter this, I'd keep the above to work out the number of minutes, use the number of minutes to negate the multiple of 60 from the total number of seconds. Leaving you with the left over seconds.
vbnet Code:
If (wc/3) >= 60 Then minutes = Math.Round(wc / 60, 0) seconds = wc - (minutes * 60) ToolStripLabel1.Text = "[TL: " & minutes & ":" & seconds"]" 'total word count Else ToolStripLabel1.Text = "[TL: " & Math.Round(wc / 3, 0) & "]" 'total word count
Above is a rough version that I typed without testing which would leave you with something like TL: 1:4. Obviously you'll have to tweak this so that it'll show 1:04, but I'm not doing everything for you
-
Jan 17th, 2012, 11:18 AM
#6
Thread Starter
Addicted Member
Re: Word Count
Thank you both! Think I've nearly got it
Thanks!
Tags for this Thread
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
|