Can somebody tell me how I would get started counting the total number of "a"s in a text box as text is entered (realtime) ? Thanks.
Printable View
Can somebody tell me how I would get started counting the total number of "a"s in a text box as text is entered (realtime) ? Thanks.
I wrote a function, although inefficient to do this, I dont know a better way:D maybe someone can come up with a more efficient way, I'm too dumb to do that:DQuote:
Originally posted by andrew104
Can somebody tell me how I would get started counting the total number of "a"s in a text box as text is entered (realtime) ? Thanks.
VB Code:
' Counts the number of times that character c is used in the string txt Function CountChars(ByVal txt As String, ByVal c As Char) As Integer Dim i As Integer For i = 0 To txt.Length - 1 If txt.Substring(i, 1) = c Then CountChars += 1 End If Next End Function
so you can call this function like:
CountChars (textBox1.text, "a"c ) ' returns the number of times character a is used in textbox1
you can call this function in textbox's Changed event if you want it to be "realtime"
Thank you for the help. :) I have figured out the single character,
however...
1. I'm now having trouble with counting the instances of "st".
I've tried an if statement (see code) and it counts correctly,
however if "s" is not the last character, then it displays "0" in the
label again. Any ideas?
2. Also, with the functions that I'm using, is there an easier way
to count lower and uppercase letters, rather than having
separate functions for each?
3. I need to count letters (A-Z). How can I keep a count of
those?
4. I also need to count words. Meaning a group of letters with a
space before and after them. Any ideas?
Thank you so much for your help! :) :D :) :D
VB Code:
' Count the number of times that character a is used in the string InputText Function strCountA1(ByVal InputText As String, ByVal a As Char) As Integer Dim i As Integer For i = 0 To InputText.Length - 1 If InputText.Substring(i, 1) = a Then strCountA1 = strCountA1 + 1 End If Next End Function ' Count the number of times that character A is used in the string InputText Function strCountA2(ByVal InputText As String, ByVal a As Char) As Integer Dim i As Integer For i = 0 To InputText.Length - 1 If InputText.Substring(i, 1) = a Then strCountA2 = strCountA2 + 1 End If Next End Function ' Count the number of times that character e is used in the string InputText Function strCountE1(ByVal InputText As String, ByVal e As Char) As Integer Dim i As Integer For i = 0 To InputText.Length - 1 If InputText.Substring(i, 1) = e Then strCountE1 = strCountE1 + 1 End If Next End Function ' Count the number of times that character E is used in the string InputText Function strCountE2(ByVal InputText As String, ByVal e As Char) As Integer Dim i As Integer For i = 0 To InputText.Length - 1 If InputText.Substring(i, 1) = e Then strCountE2 = strCountE2 + 1 End If Next End Function ' Count the number of times that character s is used in the string InputText Function strCountS1(ByVal InputText As String, ByVal s As Char) As Integer Dim i As Integer For i = 0 To InputText.Length - 1 If InputText.Substring(i, 1) = s Then strCountS1 = strCountS1 + 1 End If Next End Function ' Count the number of times that character S is used in the string InputText Function strCountS2(ByVal InputText As String, ByVal s As Char) As Integer Dim i As Integer For i = 0 To InputText.Length - 1 If InputText.Substring(i, 1) = s Then strCountS2 = strCountS2 + 1 End If Next End Function ' Count the number of times that character v is used in the string InputText Function strCountV1(ByVal InputText As String, ByVal s As Char) As Integer Dim i As Integer For i = 0 To InputText.Length - 1 If InputText.Substring(i, 1) = s Then strCountV1 = strCountV1 + 1 End If Next End Function ' Count the number of times that character V is used in the string InputText Function strCountV2(ByVal InputText As String, ByVal s As Char) As Integer Dim i As Integer For i = 0 To InputText.Length - 1 If InputText.Substring(i, 1) = s Then strCountV2 = strCountV2 + 1 End If Next End Function ' Count the number of times that characters st are used in the string InputText Function CountSTTotal(ByVal InputText As String, ByVal t As Char) As Integer Dim i As Integer If InputText.EndsWith("s") Or InputText.EndsWith("S") Then For i = 0 To InputText.Length - 1 If InputText.Substring(i, 1) = t Then CountSTTotal = CountSTTotal + 1 End If Next End If End Function 'test countwords Function CountWords() Dim strWords() As String Dim lCounter As Long Dim lWordCount As Long strWords = Split(Trim(InputTextBox.Text), " ") For lCounter = 0 To UBound(strWords) If strWords(lCounter) <> " " And strWords(lCounter) <> "" Then lWordCount = lWordCount + 1 Next lCounter End Function Private Sub InputTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InputTextBox.TextChanged Collection1.Add(Me.InputTextBox) Me.ClearButton.Enabled = True Me.ACountLabel.Text = strCountA1(InputTextBox.Text, "a"c) + strCountA2(InputTextBox.Text, "A"c) Me.ECountLabel.Text = strCountE1(InputTextBox.Text, "e"c) + strCountE2(InputTextBox.Text, "E"c) Me.SCountLabel.Text = strCountS1(InputTextBox.Text, "s"c) + strCountS2(InputTextBox.Text, "S"c) Me.VCountLabel.Text = strCountV1(InputTextBox.Text, "v"c) + strCountV2(InputTextBox.Text, "V"c) Me.STCountLabel.Text = CountSTTotal(InputTextBox.Text, "t"c) Me.LetterCountLabel.Text = intLetterCount End Sub 'test count letters Private Sub InputTextBox_KeyPress(ByVal KeyAscii As Integer) If (KeyAscii > 64 And KeyAscii < 91) Or (KeyAscii > 96 And KeyAscii < 123) Then 'user typed a letter intLetterCount = intLetterCount + 1 Me.LetterCountLabel.Text = intLetterCount End If End Sub
Anybody???
aah sorry that I'm not answering your question, I didnt even read what you asked (dont have vb right now... ):D
but you're doing something wrong:D:D:D:D
when I said it "counts the number of times character c is used," I ment that c as a variable, and not "c"
You dont need to make many different functions to count different characters. Just use the ONE function that I wrote. You can use it like this:
look for character "a" -> CountChars (textBox1.text, "a"c )
look for character "b" -> CountChars (textBox1.text, "b"c )
look for character "c" -> CountChars (textBox1.text, "c"c )
look for character "d" -> CountChars (textBox1.text, "d"c )
look for character "e" -> CountChars (textBox1.text, "e"c )
one function is enought, makes sense?:D
when I use something like "a"c, the c means that VB should threat "a" as a character and not as string. I dont know any other way to do this, I'm dumb:D maybe it's the only way to do it. In C++ you can put characters in ' (like 'a' , instead of "a"c )