r/visualbasic • u/[deleted] • Mar 30 '12
Learning VB and trying to write a program will count the number of times a certain letter appears in a textbox. I'm lost and need help!
Hey guys,
I know this may be an easier one, but I'm completely lost. I have some of the program written but am stuck with the rest of it. Here is what I need the program to do. The stuff written so far may need to be scrapped because I'm completely lost.
There are checkboxes for the letters that need to be found (A, E, I, O, U, and ST). When a checkbox is selected I need for the program to find the number of those letters and then display the number in a label.
I have this written up for code so far, but I am lost with even if the code written is correct. I have no idea how to display the count of each individual letter in an individual box. I have also attached a screenshot of the interface to give you all a better idea of what I need.
And here is my code (I apologize in advance for any stupid mistakes made):
Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click
Dim intText As Integer
Dim word As String
Dim sChar As String
Dim i As Integer
word = Me.txtScan.Text
If cbxWord.Checked = True Then
intText = txtScan.TextLength
lblWords.Text = intText.ToString("n0")
End If
Do While Boolean.TrueString
If cbxA.Checked = True Then
txtScan.Text.Contains("A, a")
End If
For i = 1 To Len(word)
sChar = LCase(Mid(word, i, 1))
If cbxA.Checked = True And sChar = "a" Then
intText = intText + 1
End If
Next i
For i = 1 To Len(word)
sChar = LCase(Mid(word, i, 1))
If cbxE.Checked = True And sChar = "e" Then
intText = intText + 1
End If
Next i
For i = 1 To Len(word)
sChar = LCase(Mid(word, i, 1))
If cbxI.Checked = True And sChar = "i" Then
intText = intText + 1
End If
Next i
For i = 1 To Len(word)
sChar = LCase(Mid(word, i, 1))
If cbxO.Checked = True And sChar = "o" Then
intText = intText + 1
End If
Next i
For i = 1 To Len(word)
sChar = LCase(Mid(word, i, 1))
If cbxU.Checked = True And sChar = "u" Then
intText = intText + 1
End If
Next i
For i = 1 To Len(word)
sChar = LCase(Mid(word, i, 1))
If cbxST.Checked = True And sChar = "st" Then
intText = intText + 1
End If
Next i
MsgBox("None Found" & intText)
Loop
End SubUPDATE Got it to work by putting a -1 next to For i = 1 To Len(word) and by deleting the loop. Thanks all for your help!!
1
u/[deleted] Mar 30 '12
Update 1 (along with suggestions in a cross post I made to learnprogramming) here is what I have... But it still isn't working 100%
When I try and use this code and hit the compute button I get a message box that will not go away. If I delete the message box then nothing computes and all the labels stay empty. Do you have any idea why this might be? Everything works perfectly if I keep the message box in there, expect for the fact that it will not go away. :/
Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click Dim intText As Integer Dim intTextA As Integer Dim intTextE As Integer Dim intTextI As Integer Dim intTextO As Integer Dim intTextU As Integer Dim intTextST As Integer Dim word As String Dim sChar As String Dim i As Integer word = Me.txtScan.Text
End Sub