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

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

→ More replies (1)

2

u/seadave77 Mar 30 '12

I know this is solved but I wanted to point out an easier way to do this using LINQ.

Dim sq = From p In TextBox1.Text Where Char.ToString(p) = "A" Select p
Debug.Print(sq.Count)

LINQ will search the string for you and if you output the count of the results (sq) you will have your answer. If you want a one-liner you could do something like this:

Debug.Print((From p In TextBox1.Text Where Char.ToString(p) = "A" Select p).Count)

1

u/CalBearFan VB.Net Advanced Mar 30 '12

Well first of all you never exit the Do Loop. You need an Exit Do in there somewhere. I can't even see why you need a Loop at all but I don't have the energy tonight to debug the code.

1

u/KeepForgettingLogin Mar 30 '12 edited Mar 30 '12

It's been a while since I've used VB, but I'll give it a bash.

First of all, what is the 'Do While Boolean.TrueString' loop supposed to do? As far as I can see, it just creates an endless loop.

Secondly, use a different variable for each of the letter counts, rather than all just intText. Then output those unique variables to the appropriate labels. (Or you could do 'the-appropriate-label.text = str(intText)' and then 'intText = 0' and the end of each loop).

Also, for "st", sChar is always going to equal one letter 'LCase(Mid(word, i, 1))', so it'll never equal "st". It should be 'sChar = LCase(Mid(word, i, 2))'

As for counting the amount of words, I'm unsure. I can only think of stupidly long winded ways. Perhaps just use the same search loop for the letters, but search for " " (space) instead?

Edit: I just realised ''sChar = LCase(Mid(word, i, 2))'' may cause problems when it gets to the last letter (although I'm unsure if it will or not, I haven't tested it). If it does, try making the loop To Len(word) - 1.

1

u/happylittlecodes Mar 30 '12

This would actually be an acceptable use for regular expressions ... but if you're struggling with something like this, regular expressions will make you suicidal.