Results 1 to 6 of 6

Thread: [RESOLVED] Word Count

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    Resolved [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

  2. #2
    Addicted Member Lectere's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    222

    Post Re: Word Count

    You need to construc a timespan, using total seconds...

    Quote Originally Posted by MacShand View Post
    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    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.

  4. #4
    Addicted Member Lectere's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    222

    Re: Word Count

    To chew it out even more for you;

    Code:
    MsgBox((New TimeSpan(0, 0, 630)).ToString)
    Quote Originally Posted by MacShand View Post
    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.

  5. #5
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    278

    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:
    1. If (wc/3) >= 60 Then
    2.  
    3. minutes = Math.Round(wc / 60, 0)
    4. seconds = wc - (minutes * 60)
    5.  
    6. ToolStripLabel1.Text = "[TL: " & minutes & ":" & seconds"]"  'total word count
    7.  
    8. Else
    9.  
    10. 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

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    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
  •  



Click Here to Expand Forum to Full Width