Jump to content

Shortening code in visual basic

freez82

This code is part of a hangman game I am working on in visual basic 2010 express, but I can't figure out how to shorten the code. Any ideas?

 


Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 'Letter A
        Dim wordLength As Integer
        wordLength = TextBox1.Text.Length

        Dim myString As String = TextBox1.Text
        Dim myChar As Char

        If wordLength = 1 Then '1
            myChar = myString.Chars(0)
            If myChar = "A" Then
                Label1.Text = "A"
            ElseIf myChar = "a" Then
                Label1.Text = "a"
            End If
        End If

        If wordLength = 2 Then '2
            myChar = myString.Chars(0)
            If myChar = "A" Then
                Label1.Text = "A"
            ElseIf myChar = "a" Then
                Label1.Text = "a"
            End If
        End If

        If wordLength = 2 Then '2
            myChar = myString.Chars(1)
            If myChar = "A" Then
                Label2.Text = "A"
            ElseIf myChar = "a" Then
                Label2.Text = "a"
            End If
        End If
 
        If wordLength = 3 Then '3
            myChar = myString.Chars(0)
            If myChar = "A" Then
                Label1.Text = "A"
            ElseIf myChar = "a" Then
                Label1.Text = "a"
            End If
        End If

        If wordLength = 3 Then '3
            myChar = myString.Chars(1)
            If myChar = "A" Then
                Label2.Text = "A"
            ElseIf myChar = "a" Then
                Label2.Text = "a"
            End If
        End If

        If wordLength = 3 Then '3
            myChar = myString.Chars(2)
            If myChar = "A" Then
                Label3.Text = "A"
            ElseIf myChar = "a" Then
                Label3.Text = "a"
            End If
        End If

        If wordLength = 4 Then '4
            myChar = myString.Chars(0)
            If myChar = "A" Then
                Label1.Text = "A"
            ElseIf myChar = "a" Then
                Label1.Text = "a"
            End If
        End If

        If wordLength = 4 Then '4
            myChar = myString.Chars(1)
            If myChar = "A" Then
                Label2.Text = "A"
            ElseIf myChar = "a" Then
                Label2.Text = "a"
            End If
        End If

        If wordLength = 4 Then '4
            myChar = myString.Chars(2)
            If myChar = "A" Then
                Label3.Text = "A"
            ElseIf myChar = "a" Then
                Label3.Text = "a"
            End If
        End If

        If wordLength = 4 Then '4
            myChar = myString.Chars(3)
            If myChar = "A" Then
                Label4.Text = "A"
            ElseIf myChar = "a" Then
                Label4.Text = "a"
            End If
        End If

        If wordLength = 5 Then '5
            myChar = myString.Chars(0)
            If myChar = "A" Then
                Label1.Text = "A"
            ElseIf myChar = "a" Then
                Label1.Text = "a"
            End If
        End If

        If wordLength = 5 Then '5
            myChar = myString.Chars(1)
            If myChar = "A" Then
                Label2.Text = "A"
            ElseIf myChar = "a" Then
                Label2.Text = "a"
            End If
        End If

        If wordLength = 5 Then '5
            myChar = myString.Chars(2)
            If myChar = "A" Then
                Label3.Text = "A"
            ElseIf myChar = "a" Then
                Label3.Text = "a"
            End If
        End If

        If wordLength = 5 Then '5
            myChar = myString.Chars(3)
            If myChar = "A" Then
                Label4.Text = "A"
            ElseIf myChar = "a" Then
                Label4.Text = "a"
            End If
        End If

        If wordLength = 5 Then '5
            myChar = myString.Chars(4)
            If myChar = "A" Then
                Label5.Text = "A"
            ElseIf myChar = "a" Then
                Label5.Text = "a"
            End If
        End If

        If wordLength = 6 Then '6
            myChar = myString.Chars(0)
            If myChar = "A" Then
                Label1.Text = "A"
            ElseIf myChar = "a" Then
                Label1.Text = "a"
            End If
        End If

        If wordLength = 6 Then '6
            myChar = myString.Chars(1)
            If myChar = "A" Then
                Label2.Text = "A"
            ElseIf myChar = "a" Then
                Label2.Text = "a"
            End If
        End If

        If wordLength = 6 Then '6
            myChar = myString.Chars(2)
            If myChar = "A" Then
                Label3.Text = "A"
            ElseIf myChar = "a" Then
                Label3.Text = "a"
            End If
        End If

        If wordLength = 6 Then '6
            myChar = myString.Chars(3)
            If myChar = "A" Then
                Label4.Text = "A"
            ElseIf myChar = "a" Then
                Label4.Text = "a"
            End If
        End If

        If wordLength = 6 Then '6
            myChar = myString.Chars(4)
            If myChar = "A" Then
                Label5.Text = "A"
            ElseIf myChar = "a" Then
                Label5.Text = "a"
            End If
        End If

        If wordLength = 6 Then '6
            myChar = myString.Chars(5)
            If myChar = "A" Then
                Label6.Text = "A"
            ElseIf myChar = "a" Then
                Label6.Text = "a"
            End If
        End If

        If wordLength = 7 Then '7
            myChar = myString.Chars(0)
            If myChar = "A" Then
                Label1.Text = "A"
            ElseIf myChar = "a" Then
                Label1.Text = "a"
            End If
        End If

        If wordLength = 7 Then '7
            myChar = myString.Chars(1)
            If myChar = "A" Then
                Label2.Text = "A"
            ElseIf myChar = "a" Then
                Label2.Text = "a"
            End If
        End If

        If wordLength = 7 Then '7
            myChar = myString.Chars(2)
            If myChar = "A" Then
                Label3.Text = "A"
            ElseIf myChar = "a" Then
                Label3.Text = "a"
            End If
        End If

        If wordLength = 7 Then '7
            myChar = myString.Chars(3)
            If myChar = "A" Then
                Label4.Text = "A"
            ElseIf myChar = "a" Then
                Label4.Text = "a"
            End If
        End If

        If wordLength = 7 Then '7
            myChar = myString.Chars(4)
            If myChar = "A" Then
                Label5.Text = "A"
            ElseIf myChar = "a" Then
                Label5.Text = "a"
            End If
        End If

        If wordLength = 7 Then '7
            myChar = myString.Chars(5)
            If myChar = "A" Then
                Label6.Text = "A"
            ElseIf myChar = "a" Then
                Label6.Text = "a"
            End If
        End If

        If wordLength = 7 Then '7
            myChar = myString.Chars(6)
            If myChar = "A" Then
                Label7.Text = "A"
            ElseIf myChar = "a" Then
                Label7.Text = "a"
            End If
        End If

        If wordLength = 8 Then '8
            myChar = myString.Chars(0)
            If myChar = "A" Then
                Label1.Text = "A"
            ElseIf myChar = "a" Then
                Label1.Text = "a"
            End If
        End If

        If wordLength = 8 Then '8
            myChar = myString.Chars(1)
            If myChar = "A" Then
                Label2.Text = "A"
            ElseIf myChar = "a" Then
                Label2.Text = "a"
            End If
        End If

        If wordLength = 8 Then '8
            myChar = myString.Chars(2)
            If myChar = "A" Then
                Label3.Text = "A"
            ElseIf myChar = "a" Then
                Label3.Text = "a"
            End If
        End If

        If wordLength = 8 Then '8
            myChar = myString.Chars(3)
            If myChar = "A" Then
                Label4.Text = "A"
            ElseIf myChar = "a" Then
                Label4.Text = "a"
            End If
        End If

        If wordLength = 8 Then '8
            myChar = myString.Chars(4)
            If myChar = "A" Then
                Label5.Text = "A"
            ElseIf myChar = "a" Then
                Label5.Text = "a"
            End If
        End If

        If wordLength = 8 Then '8
            myChar = myString.Chars(5)
            If myChar = "A" Then
                Label6.Text = "A"
            ElseIf myChar = "a" Then
                Label6.Text = "a"
            End If
        End If

        If wordLength = 8 Then '8
            myChar = myString.Chars(6)
            If myChar = "A" Then
                Label7.Text = "A"
            ElseIf myChar = "a" Then
                Label7.Text = "a"
            End If
        End If

        If wordLength = 8 Then '8
            myChar = myString.Chars(7)
            If myChar = "A" Then
                Label8.Text = "A"
            ElseIf myChar = "a" Then
                Label8.Text = "a"
            End If
        End If

        If wordLength = 9 Then '9
            myChar = myString.Chars(0)
            If myChar = "A" Then
                Label1.Text = "A"
            ElseIf myChar = "a" Then
                Label1.Text = "a"
            End If
        End If

        If wordLength = 9 Then '9
            myChar = myString.Chars(1)
            If myChar = "A" Then
                Label2.Text = "A"
            ElseIf myChar = "a" Then
                Label2.Text = "a"
            End If
        End If

        If wordLength = 9 Then '9
            myChar = myString.Chars(2)
            If myChar = "A" Then
                Label3.Text = "A"
            ElseIf myChar = "a" Then
                Label3.Text = "a"
            End If
        End If

        If wordLength = 9 Then '9
            myChar = myString.Chars(3)
            If myChar = "A" Then
                Label4.Text = "A"
            ElseIf myChar = "a" Then
                Label4.Text = "a"
            End If
        End If

        If wordLength = 9 Then '9
            myChar = myString.Chars(4)
            If myChar = "A" Then
                Label5.Text = "A"
            ElseIf myChar = "a" Then
                Label5.Text = "a"
            End If
        End If

        If wordLength = 9 Then '9
            myChar = myString.Chars(5)
            If myChar = "A" Then
                Label6.Text = "A"
            ElseIf myChar = "a" Then
                Label6.Text = "a"
            End If
        End If

        If wordLength = 9 Then '9
            myChar = myString.Chars(6)
            If myChar = "A" Then
                Label7.Text = "A"
            ElseIf myChar = "a" Then
                Label7.Text = "a"
            End If
        End If

        If wordLength = 9 Then '9
            myChar = myString.Chars(7)
            If myChar = "A" Then
                Label8.Text = "A"
            ElseIf myChar = "a" Then
                Label8.Text = "a"
            End If
        End If

        If wordLength = 9 Then '9
            myChar = myString.Chars(8)
            If myChar = "A" Then
                Label9.Text = "A"
            ElseIf myChar = "a" Then
                Label9.Text = "a"
            End If
        End If

        If wordLength = 10 Then '10
            myChar = myString.Chars(0)
            If myChar = "A" Then
                Label1.Text = "A"
            ElseIf myChar = "a" Then
                Label1.Text = "a"
            End If
        End If

        If wordLength = 10 Then '10
            myChar = myString.Chars(1)
            If myChar = "A" Then
                Label2.Text = "A"
            ElseIf myChar = "a" Then
                Label2.Text = "a"
            End If
        End If

        If wordLength = 10 Then '10
            myChar = myString.Chars(2)
            If myChar = "A" Then
                Label3.Text = "A"
            ElseIf myChar = "a" Then
                Label3.Text = "a"
            End If
        End If

        If wordLength = 10 Then '10
            myChar = myString.Chars(3)
            If myChar = "A" Then
                Label4.Text = "A"
            ElseIf myChar = "a" Then
                Label4.Text = "a"
            End If
        End If

        If wordLength = 10 Then '10
            myChar = myString.Chars(4)
            If myChar = "A" Then
                Label5.Text = "A"
            ElseIf myChar = "a" Then
                Label5.Text = "a"
            End If
        End If

        If wordLength = 10 Then '10
            myChar = myString.Chars(5)
            If myChar = "A" Then
                Label6.Text = "A"
            ElseIf myChar = "a" Then
                Label6.Text = "a"
            End If
        End If

        If wordLength = 10 Then '10
            myChar = myString.Chars(6)
            If myChar = "A" Then
                Label7.Text = "A"
            ElseIf myChar = "a" Then
                Label7.Text = "a"
            End If
        End If

        If wordLength = 10 Then '10
            myChar = myString.Chars(7)
            If myChar = "A" Then
                Label8.Text = "A"
            ElseIf myChar = "a" Then
                Label8.Text = "a"
            End If
        End If

        If wordLength = 10 Then '10
            myChar = myString.Chars(8)
            If myChar = "A" Then
                Label9.Text = "A"
            ElseIf myChar = "a" Then
                Label9.Text = "a"
            End If
        End If

        If wordLength = 10 Then '10
            myChar = myString.Chars(9)
            If myChar = "A" Then
                Label10.Text = "A"
            ElseIf myChar = "a" Then
                Label10.Text = "a"
            End If
        End If

        If wordLength = 11 Then '11
            myChar = myString.Chars(0)
            If myChar = "A" Then
                Label1.Text = "A"
            ElseIf myChar = "a" Then
                Label1.Text = "a"
            End If
        End If

        If wordLength = 11 Then '11
            myChar = myString.Chars(1)
            If myChar = "A" Then
                Label2.Text = "A"
            ElseIf myChar = "a" Then
                Label2.Text = "a"
            End If
        End If

        If wordLength = 11 Then '11
            myChar = myString.Chars(2)
            If myChar = "A" Then
                Label3.Text = "A"
            ElseIf myChar = "a" Then
                Label3.Text = "a"
            End If
        End If

        If wordLength = 11 Then '11
            myChar = myString.Chars(3)
            If myChar = "A" Then
                Label4.Text = "A"
            ElseIf myChar = "a" Then
                Label4.Text = "a"
            End If
        End If

        If wordLength = 11 Then '11
            myChar = myString.Chars(4)
            If myChar = "A" Then
                Label5.Text = "A"
            ElseIf myChar = "a" Then
                Label5.Text = "a"
            End If
        End If

        If wordLength = 11 Then '11
            myChar = myString.Chars(5)
            If myChar = "A" Then
                Label6.Text = "A"
            ElseIf myChar = "a" Then
                Label6.Text = "a"
            End If
        End If

        If wordLength = 11 Then '11
            myChar = myString.Chars(6)
            If myChar = "A" Then
                Label7.Text = "A"
            ElseIf myChar = "a" Then
                Label7.Text = "a"
            End If
        End If

        If wordLength = 11 Then '11
            myChar = myString.Chars(7)
            If myChar = "A" Then
                Label8.Text = "A"
            ElseIf myChar = "a" Then
                Label8.Text = "a"
            End If
        End If

        If wordLength = 11 Then '11
            myChar = myString.Chars(8)
            If myChar = "A" Then
                Label9.Text = "A"
            ElseIf myChar = "a" Then
                Label9.Text = "a"
            End If
        End If

        If wordLength = 11 Then '11
            myChar = myString.Chars(9)
            If myChar = "A" Then
                Label10.Text = "A"
            ElseIf myChar = "a" Then
                Label10.Text = "a"
            End If
        End If

        If wordLength = 11 Then '11
            myChar = myString.Chars(10)
            If myChar = "A" Then
                Label11.Text = "A"
            ElseIf myChar = "a" Then
                Label11.Text = "a"
            End If
        End If

        If wordLength = 12 Then '12
            myChar = myString.Chars(0)
            If myChar = "A" Then
                Label1.Text = "A"
            ElseIf myChar = "a" Then
                Label1.Text = "a"
            End If
        End If

        If wordLength = 12 Then '12
            myChar = myString.Chars(1)
            If myChar = "A" Then
                Label2.Text = "A"
            ElseIf myChar = "a" Then
                Label2.Text = "a"
            End If
        End If

        If wordLength = 12 Then '12
            myChar = myString.Chars(2)
            If myChar = "A" Then
                Label3.Text = "A"
            ElseIf myChar = "a" Then
                Label3.Text = "a"
            End If
        End If

        If wordLength = 12 Then '12
            myChar = myString.Chars(3)
            If myChar = "A" Then
                Label4.Text = "A"
            ElseIf myChar = "a" Then
                Label4.Text = "a"
            End If
        End If

        If wordLength = 12 Then '12
            myChar = myString.Chars(4)
            If myChar = "A" Then
                Label5.Text = "A"
            ElseIf myChar = "a" Then
                Label5.Text = "a"
            End If
        End If

        If wordLength = 12 Then '12
            myChar = myString.Chars(5)
            If myChar = "A" Then
                Label6.Text = "A"
            ElseIf myChar = "a" Then
                Label6.Text = "a"
            End If
        End If

        If wordLength = 12 Then '12
            myChar = myString.Chars(6)
            If myChar = "A" Then
                Label7.Text = "A"
            ElseIf myChar = "a" Then
                Label7.Text = "a"
            End If
        End If

        If wordLength = 12 Then '12
            myChar = myString.Chars(7)
            If myChar = "A" Then
                Label8.Text = "A"
            ElseIf myChar = "a" Then
                Label8.Text = "a"
            End If
        End If

        If wordLength = 12 Then '12
            myChar = myString.Chars(8)
            If myChar = "A" Then
                Label9.Text = "A"
            ElseIf myChar = "a" Then
                Label9.Text = "a"
            End If
        End If

        If wordLength = 12 Then '12
            myChar = myString.Chars(9)
            If myChar = "A" Then
                Label10.Text = "A"
            ElseIf myChar = "a" Then
                Label10.Text = "a"
            End If
        End If

        If wordLength = 12 Then '12
            myChar = myString.Chars(10)
            If myChar = "A" Then
                Label11.Text = "A"
            ElseIf myChar = "a" Then
                Label11.Text = "a"
            End If
        End If

        If wordLength = 12 Then '12
            myChar = myString.Chars(11)
            If myChar = "A" Then
                Label12.Text = "A"
            ElseIf myChar = "a" Then
                Label12.Text = "a"
            End If
        End If

 

Link to comment
Share on other sites

Link to post
Share on other sites

Holy moly, you wrote all this?

You need to put this stuff in a "for loop" 

 

Although I've forgotten a lot about VB so I'm not sure how you'd dynamically address each of those labels. Sorry if this isn't much help but you can get everything in a single loop then have if statements to work out the label.

I edit my posts a lot.

Link to comment
Share on other sites

Link to post
Share on other sites

On 2/18/2018 at 3:26 PM, MrDrWho13 said:

-snipe-

 

No, just do bunch of GOTOs.

learn from this guy

 

 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

22 minutes ago, wasab said:

No, just do bunch of GOTOs.

learn from this guy

 

 

Oh god...

I edit my posts a lot.

Link to comment
Share on other sites

Link to post
Share on other sites

Holy code smells batman!

There are a bunch of methods that you can (and should) use to make this shorter. The first would be a simple one pass refactoring. For example this snippet:

 

        If wordLength = 7 Then '7
            myChar = myString.Chars(0)
            If myChar = "A" Then
                Label1.Text = "A"
            ElseIf myChar = "a" Then
                Label1.Text = "a"
            End If
        End If

        If wordLength = 7 Then '7
            myChar = myString.Chars(1)
            If myChar = "A" Then
                Label2.Text = "A"
            ElseIf myChar = "a" Then
                Label2.Text = "a"
            End If
        End If

        If wordLength = 7 Then '7
            myChar = myString.Chars(2)
            If myChar = "A" Then
                Label3.Text = "A"
            ElseIf myChar = "a" Then
                Label3.Text = "a"
            End If
        End If

        If wordLength = 7 Then '7
            myChar = myString.Chars(3)
            If myChar = "A" Then
                Label4.Text = "A"
            ElseIf myChar = "a" Then
                Label4.Text = "a"
            End If
        End If

        If wordLength = 7 Then '7
            myChar = myString.Chars(4)
            If myChar = "A" Then
                Label5.Text = "A"
            ElseIf myChar = "a" Then
                Label5.Text = "a"
            End If
        End If

        If wordLength = 7 Then '7
            myChar = myString.Chars(5)
            If myChar = "A" Then
                Label6.Text = "A"
            ElseIf myChar = "a" Then
                Label6.Text = "a"
            End If
        End If

        If wordLength = 7 Then '7
            myChar = myString.Chars(6)
            If myChar = "A" Then
                Label7.Text = "A"
            ElseIf myChar = "a" Then
                Label7.Text = "a"
            End If
        End If


Can become one if statement. It's simple logic, if "wordLength = 7" evaluates to true, then it evaluates to true. No need to immediately check it again. Then you can use an embedded loop to roll through the indexes and apply the action to them. 
 

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

You don't seem to be that experienced, but here's a somewhat "advanced" suggestion anyway:

 

You could loop through the alphabet (A-Z) and for each letter add a button to the form. For each button set the name and text to the current letter and assign the click event of the button a shared method that evaluates the clicked letter up against the word.

 

Here's a quick example without positioning on the form:

Private Sub AddButtons()
' Loop through the alphabet (A-Z).
        For letter = Asc("A") To Asc("Z")
            ' We create a new button and set it's name and text to the current letter of the alphabet.
            Dim button As New Button
            button.Name = "btn" + Chr(letter)
            button.Text = Chr(letter)

            ' We set the buttons click event to a shared method.
            AddHandler button.Click, AddressOf EvaluateGuess
        Next
End Sub

Private Sub EvaluateGuess(sender As Object, e As EventArgs)
End Sub

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×