By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,637 Members | 1,945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,637 IT Pros & Developers. It's quick & easy.

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

P: 9
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.

Expand|Select|Wrap|Line Numbers
  1. ''' <summary>
  2.     ''' Returns Word Count base on spaces
  3.     ''' </summary>
  4.     ''' <param name="textToParse"></param>
  5.     ''' <returns>Integer = Word Count</returns>
  6.     ''' <remarks></remarks>
  7.     Private Function CountWords(ByVal textToParse As String) As Integer
  8.         Dim intWords = 0
  9.         If textToParse.Trim.Length > 0 Then
  10.             Dim strWords() As String = textToParse.Split(" "c)
  11.             intWords = strWords.Length
  12.         End If
  13.         Return intWords
  14.     End Function
  15.  
  16. Private Sub txtWords_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtWords.TextChanged
  17.         UpdateDisplay()
  18.         'lblResults.Text = "The results are: " & CountCharacters(txtWords.Text).ToString("N0")
  19.     End Sub
  20.  
  21.     Private Sub UpdateDisplay()
  22.         If radCountChars.Checked Then
  23.             lblResults.Text = "Character Count is: " & CountCharacters(txtWords.Text).ToString("N0") & " chars."
  24.         Else
  25.             lblResults.Text = "Word Count is: " & CountWords(txtWords.Text).ToString("N0") & " words."
  26.         End If
  27.     End Sub
  28.  
  29.  
Oct 7 '08 #1
Share this Question
Share on Google+
5 Replies


P: 77
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(ByVal textToParse As String) As Integer
Dim intWords = 0
Dim strWords As New List(Of String)
If textToParse.Trim.Length > 0 Then
strWords.AddRange(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_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

/[code]
Oct 8 '08 #2

P: 9
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(ByVal textToParse As String) As Integer
Dim intWords = 0
Dim strWords As New List(Of String)
If textToParse.Trim.Length > 0 Then
strWords.AddRange(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_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

/[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,

Oct 8 '08 #3

P: 77
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.AcceptsReturn = 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.KeyEventArgs) 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
Oct 9 '08 #4

P: 9
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.     ''' <summary>
  4.     ''' Return number of characters in string
  5.     ''' </summary>
  6.     ''' <param name="textToCount"></param>
  7.     ''' <returns>Integer = number of characters</returns>
  8.     ''' <remarks></remarks>
  9.     Private Function CountCharacters(ByVal textToCount As String) As Integer
  10.         Return textToCount.Length
  11.     End Function
  12.     ''' <summary>
  13.     ''' Returns Word Count base on spaces
  14.     ''' </summary>
  15.     ''' <param name="textToParse"></param>
  16.     ''' <returns>Integer = Word Count</returns>
  17.     ''' <remarks></remarks>
  18.     Private Function CountWords(ByVal textToParse As String) As Integer
  19.         Dim intWords = 0
  20.         If textToParse.Trim.Length > 0 Then
  21.             Dim strWords() As String = textToParse.Split(" "c)
  22.             intWords = strWords.Length
  23.         End If
  24.         Return intWords
  25.     End Function
  26.  
  27.     Private Sub UpdateDisplay()
  28.         If radCountChars.Checked Then
  29.             lblResults.Text = "Character Count is: " & CountCharacters(txtWords.Text).ToString("N0") & " chars."
  30.         Else
  31.             lblResults.Text = "Word Count is: " & CountWords(txtWords.Text).ToString("N0") & " words."
  32.         End If
  33.     End Sub
  34.  
  35.     Private Sub txtWords_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtWords.TextChanged
  36.         UpdateDisplay()
  37.         'lblResults.Text = "The results are: " & CountCharacters(txtWords.Text).ToString("N0")
  38.     End Sub
  39.  
  40.     Private Sub UpdateDisplay()
  41.         If radCountChars.Checked Then
  42.             lblResults.Text = "Character Count is: " & CountCharacters(txtWords.Text).ToString("N0") & " chars."
  43.         Else
  44.             lblResults.Text = "Word Count is: " & CountWords(txtWords.Text).ToString("N0") & " words."
  45.         End If
  46.     End Sub
  47.  
  48.  
  49.     Private Sub radCountChars_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radCountChars.CheckedChanged
  50.         UpdateDisplay()
  51.     End Sub
  52.  
  53.     Private Sub radCountWords_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radCountWords.CheckedChanged
  54.         UpdateDisplay()
  55.     End Sub
  56. End Class
  57.  
Here is my whole code. Thanks
Oct 9 '08 #5

P: 77
The code I've submit you works?
Oct 10 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.