How can I count "Enter key" as a String in a WinFrmWordCounter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gflor16
    New Member
    • Sep 2008
    • 9

    How can I count "Enter key" as a String in a WinFrmWordCounter

    Problem: I have this code to run a word counter. But I have a problem when I hit the enter key, it doesn't give me any output of how many chars or words.

    Code:
    ''' <summary>
        ''' Returns Word Count base on spaces
        ''' </summary>
        ''' <param name="textToParse"></param>
        ''' <returns>Integer = Word Count</returns>
        ''' <remarks></remarks>
        Private Function CountWords(ByVal textToParse As String) As Integer
            Dim intWords = 0
            If textToParse.Trim.Length > 0 Then
                Dim strWords() As String = textToParse.Split(" "c)
                intWords = strWords.Length
            End If
            Return intWords
        End Function
    
    Private Sub txtWords_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtWords.TextChanged
            UpdateDisplay()
            'lblResults.Text = "The results are: " & CountCharacters(txtWords.Text).ToString("N0")
        End Sub
    
        Private Sub UpdateDisplay()
            If radCountChars.Checked Then
                lblResults.Text = "Character Count is: " & CountCharacters(txtWords.Text).ToString("N0") & " chars."
            Else
                lblResults.Text = "Word Count is: " & CountWords(txtWords.Text).ToString("N0") & " words."
            End If
        End Sub
  • rpicilli
    New Member
    • Aug 2008
    • 77

    #2
    Hi there

    I saw in your code some "bugs". If you allow me I change your code a little bit.

    In your code the space key is counted as a word! I change that. I do not understant your question about the EnterKey. Try to put in another way so I'll try to help you.

    I hope this code help

    Rpicilli

    [CODE]

    Private Function CountWords(ByVa l textToParse As String) As Integer
    Dim intWords = 0
    Dim strWords As New List(Of String)
    If textToParse.Tri m.Length > 0 Then
    strWords.AddRan ge(textToParse. Split(" "c))
    Try
    For Each s As String In strWords
    If s = "" Then
    strWords.Remove ("")
    End If
    Next
    Catch ex As Exception
    End Try
    intWords = strWords.Count
    End If
    Return intWords
    End Function

    Private Sub txtWords_TextCh anged(ByVal sender As Object, ByVal e As System.EventArg s) Handles txtWords.TextCh anged
    UpdateDisplay()
    'lblResults.Tex t = "The results are: " & CountCharacters (txtWords.Text) .ToString("N0")
    End Sub

    Private Sub UpdateDisplay()
    If radCountChars.C hecked Then
    'lblResults.Tex t = "Character Count is: " & CountCharacters (txtWords.Text) .ToString("N0") & " chars."
    Else
    lblResults.Text = "Word Count is: " & CountWords(txtW ords.Text).ToSt ring("N0") & " words."
    End If
    End Sub

    /[CODE]

    Comment

    • gflor16
      New Member
      • Sep 2008
      • 9

      #3
      Originally posted by rpicilli
      Hi there

      I saw in your code some "bugs". If you allow me I change your code a little bit.

      In your code the space key is counted as a word! I change that. I do not understant your question about the EnterKey. Try to put in another way so I'll try to help you.

      I hope this code help

      Rpicilli

      [CODE]

      Private Function CountWords(ByVa l textToParse As String) As Integer
      Dim intWords = 0
      Dim strWords As New List(Of String)
      If textToParse.Tri m.Length > 0 Then
      strWords.AddRan ge(textToParse. Split(" "c))
      Try
      For Each s As String In strWords
      If s = "" Then
      strWords.Remove ("")
      End If
      Next
      Catch ex As Exception
      End Try
      intWords = strWords.Count
      End If
      Return intWords
      End Function

      Private Sub txtWords_TextCh anged(ByVal sender As Object, ByVal e As System.EventArg s) Handles txtWords.TextCh anged
      UpdateDisplay()
      'lblResults.Tex t = "The results are: " & CountCharacters (txtWords.Text) .ToString("N0")
      End Sub

      Private Sub UpdateDisplay()
      If radCountChars.C hecked Then
      'lblResults.Tex t = "Character Count is: " & CountCharacters (txtWords.Text) .ToString("N0") & " chars."
      Else
      lblResults.Text = "Word Count is: " & CountWords(txtW ords.Text).ToSt ring("N0") & " words."
      End If
      End Sub

      /[CODE]
      Here is the photo shoot of the running program. The problem when you hit EnterKey then type a next word, it don't count.

      Thanks,

      Comment

      • rpicilli
        New Member
        • Aug 2008
        • 77

        #4
        Hi again.

        Should be easy to help you if you put the code you are using. Either way I'll try.

        First put in the method LOAD of you form the following:

        [CODE]
        txtWords.Accept sReturn = True
        /[CODE]

        Second put the following code any where inside you class form.

        [CODE]
        Private Sub txtWords_KeyUp( ByVal sender As Object, ByVal e As System.Windows. Forms.KeyEventA rgs) Handles txtWords.KeyUp
        If e.KeyCode = Keys.Enter Then
        'Do something
        MessageBox.Show (e.KeyData)
        'in case you want do nothing use
        e.Handled = True
        End If
        End Sub
        /[CODE]

        Wherever you wana do, use the KeyUp method. Here you can grap the EnterKey using the code above.

        Tell me if this solve your problem.

        Rpicilli

        Comment

        • gflor16
          New Member
          • Sep 2008
          • 9

          #5
          Code:
          Public Class Form1
          
              ''' <summary>
              ''' Return number of characters in string
              ''' </summary>
              ''' <param name="textToCount"></param>
              ''' <returns>Integer = number of characters</returns>
              ''' <remarks></remarks>
              Private Function CountCharacters(ByVal textToCount As String) As Integer
                  Return textToCount.Length
              End Function
              ''' <summary>
              ''' Returns Word Count base on spaces
              ''' </summary>
              ''' <param name="textToParse"></param>
              ''' <returns>Integer = Word Count</returns>
              ''' <remarks></remarks>
              Private Function CountWords(ByVal textToParse As String) As Integer
                  Dim intWords = 0
                  If textToParse.Trim.Length > 0 Then
                      Dim strWords() As String = textToParse.Split(" "c)
                      intWords = strWords.Length
                  End If
                  Return intWords
              End Function
          
              Private Sub UpdateDisplay()
                  If radCountChars.Checked Then
                      lblResults.Text = "Character Count is: " & CountCharacters(txtWords.Text).ToString("N0") & " chars."
                  Else
                      lblResults.Text = "Word Count is: " & CountWords(txtWords.Text).ToString("N0") & " words."
                  End If
              End Sub
          
              Private Sub txtWords_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtWords.TextChanged
                  UpdateDisplay()
                  'lblResults.Text = "The results are: " & CountCharacters(txtWords.Text).ToString("N0")
              End Sub
          
              Private Sub UpdateDisplay()
                  If radCountChars.Checked Then
                      lblResults.Text = "Character Count is: " & CountCharacters(txtWords.Text).ToString("N0") & " chars."
                  Else
                      lblResults.Text = "Word Count is: " & CountWords(txtWords.Text).ToString("N0") & " words."
                  End If
              End Sub
          
          
              Private Sub radCountChars_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radCountChars.CheckedChanged
                  UpdateDisplay()
              End Sub
          
              Private Sub radCountWords_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radCountWords.CheckedChanged
                  UpdateDisplay()
              End Sub
          End Class
          Here is my whole code. Thanks

          Comment

          • rpicilli
            New Member
            • Aug 2008
            • 77

            #6
            The code I've submit you works?

            Comment

            Working...