r/visualbasic • • 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.

http://i.imgur.com/nplWK.png

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 Sub

UPDATE 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!!

5 Upvotes

6 comments sorted by

View all comments

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

Dim words() As String = Split(word, " ")
Dim NumberOfWords As Integer = words.Count

If cbxWord.Checked = True Then
    intText = txtScan.TextLength
    lblWords.Text = intText.ToString("n0")
End If

Do While Boolean.TrueString

    For i = 1 To Len(word) - 1
        sChar = LCase(Mid(word, i, 1))
        If cbxA.Checked = True And sChar = "a" Then
            intTextA = intTextA + 1
            lblAs.Text = intTextA.ToString("n0")
        End If

    Next i

    For i = 1 To Len(word) - 1
        sChar = LCase(Mid(word, i, 1))
        If cbxE.Checked = True And sChar = "e" Then
            intTextE = intTextE + 1
            lblEs.Text = intTextE.ToString("n0")
        End If
    Next i

    For i = 1 To Len(word) - 1
        sChar = LCase(Mid(word, i, 1))
        If cbxI.Checked = True And sChar = "i" Then
            intTextI = intTextI + 1
            lblIs.Text = intTextI.ToString("n0")
        End If

    Next i

    For i = 1 To Len(word) - 1
        sChar = LCase(Mid(word, i, 1))
        If cbxO.Checked = True And sChar = "o" Then
            intTextO = intTextO + 1
            lblOs.Text = intTextO.ToString("n0")
        End If

    Next i

    For i = 1 To Len(word) - 1
        sChar = LCase(Mid(word, i, 1))
        If cbxU.Checked = True And sChar = "u" Then
            intTextU = intTextU + 1
            lblUs.Text = intTextU.ToString("n0")
        End If

    Next i

    For i = 1 To Len(word) - 1
        sChar = LCase(Mid(word, i, 2))
        If cbxST.Checked = True And sChar = "st" Then
            intTextST = intTextST + 1
            lblSts.Text = intTextST.ToString("n0")
        End If

    Next i

    MessageBox.Show("None Found" & intText)

    If Boolean.FalseString Then
        Exit Do
    End If
Loop

End Sub

1

u/happylittlecodes Mar 30 '12

Ok, just get rid of the Do While loop completely -- it's not necessary. The message box actually does go away, it's just that the Do While loop (henceforth referred to as DWL) causes it to be displayed again immediately. The DWL is causing an endless loop, and that's why nothing is happening when you get rid of the message box -- because it's actually happening constantly, endlessly.

So, lose these lines:

Do While Boolean.TrueString

If Boolean.FalseString Then

    Exit Do

End If

Loop

And then see what it does. On a side note, is this pre-.NET VB, or VB.NET?