c

Public Class Form1

    Dim Word As String                      ’ will hold the word to be guessed

    Dim WordLength As Integer               ’ will hold the length of the word

    Dim Char1 As String                     ’ will hold the first letter of the word

    Dim Char2 As String                     ’ will hold the second letter of the word

    Dim Char3 As String                     ’ will hold the third letter of the word

    Dim Char4 As String                     ’ will hold the fourth letter of the word

    Dim Char5 As String                     ’ will hold the fifth letter of the word

    Dim Char6 As String                     ’ will hold the sixth letter of the word

    Dim GameStarted As Boolean = False      ’ will switch to True when the user click on Play Button

    Dim RightTrys As Integer                ’ will hold the number of the right guessed letters

    Dim WrongTrys As Integer                ’ will hold the number of the wrong guessed letters

    Public Sub DrawHanger()

        Dim pnt As Graphics

        pnt = Me.CreateGraphics

        Dim pencil As New Pen(Color.Black, 2)

        pnt.DrawLine(pencil, 46, 434, 115, 394)

        pnt.DrawLine(pencil, 115, 394, 184, 434)

        pnt.DrawLine(pencil, 115, 394, 115, 220)

        pnt.DrawLine(pencil, 115, 220, 250, 220)

        pnt.DrawLine(pencil, 250, 220, 250, 250)

    End Sub

    Public Sub resetGame()

        WrongGuessesLabel.Text = “”

        Dim pnt As Graphics

        pnt = Me.CreateGraphics

        pnt.Clear(Color.FromArgb(0, 192, 0))

        PlayButton.Visible = True

    End Sub

    Public Sub clearLetters()

        Letter1.Text = “”

        Letter2.Text = “”

        Letter3.Text = “”

        Letter4.Text = “”

        Letter5.Text = “”

        Letter6.Text = “”

    End Sub

    Public Sub CheckForWinning()

        If RightTrys = WordLength Then

            MsgBox(“You Won”)

            resetGame()

        End If

    End Sub

    Public Sub StartDrawingWhenLosing(ByVal trys As Integer)

        Dim pnt As Graphics

        pnt = Me.CreateGraphics

        Dim pencil As New Pen(Color.Black, 2)

        If trys = 1 Then

            pnt.DrawEllipse(pencil, 228, 250, 40, 40)

        ElseIf trys = 2 Then

            pnt.DrawLine(pencil, 248, 290, 248, 370)

        ElseIf trys = 3 Then

            pnt.DrawLine(pencil, 248, 315, 213, 293)

        ElseIf trys = 4 Then

            pnt.DrawLine(pencil, 248, 315, 283, 293)

        ElseIf trys = 5 Then

            pnt.DrawLine(pencil, 248, 369, 213, 391)

        ElseIf trys = 6 Then

            pnt.DrawLine(pencil, 248, 369, 283, 391)

            TextBox1.Clear()

            Try

                Letter1.Text = Char1

                Letter2.Text = Char2

                Letter3.Text = Char3

                Letter4.Text = Char4

                Letter5.Text = Char5

                Letter6.Text = Char6

            Catch ex As Exception

            End Try

            MsgBox(“You Lose”)

            resetGame()

        End If

    End Sub

    Public Sub hideCharacterLength()

        Line1.Visible = False

        Line2.Visible = False

        Line3.Visible = False

        Line4.Visible = False

        Line5.Visible = False

        Line6.Visible = False

    End Sub

    Public Sub AssignLetters()

        If WordLength = 2 Then

            Char1 = Word.Chars(0).ToString.ToUpper

            Char2 = Word.Chars(1).ToString.ToUpper

        ElseIf WordLength = 3 Then

            Char1 = Word.Chars(0).ToString.ToUpper

            Char2 = Word.Chars(1).ToString.ToUpper

            Char3 = Word.Chars(2).ToString.ToUpper

        ElseIf WordLength = 4 Then

            Char1 = Word.Chars(0).ToString.ToUpper

            Char2 = Word.Chars(1).ToString.ToUpper

            Char3 = Word.Chars(2).ToString.ToUpper

            Char4 = Word.Chars(3).ToString.ToUpper

        ElseIf WordLength = 5 Then

            Char1 = Word.Chars(0).ToString.ToUpper

            Char2 = Word.Chars(1).ToString.ToUpper

            Char3 = Word.Chars(2).ToString.ToUpper

            Char4 = Word.Chars(3).ToString.ToUpper

            Char5 = Word.Chars(4).ToString.ToUpper

        ElseIf WordLength = 6 Then

            Char1 = Word.Chars(0).ToString.ToUpper

            Char2 = Word.Chars(1).ToString.ToUpper

            Char3 = Word.Chars(2).ToString.ToUpper

            Char4 = Word.Chars(3).ToString.ToUpper

            Char5 = Word.Chars(4).ToString.ToUpper

            Char6 = Word.Chars(5).ToString.ToUpper

        End If

    End Sub

    Public Sub hideCharachterLength()

        Line1.Visible = False

        Line2.Visible = False

        Line3.Visible = False

        Line4.Visible = False

        Line5.Visible = False

        Line6.Visible = False

    End Sub

    Public Sub ShowLength(ByVal VisibleLines As Integer)

        hideCharachterLength()

        If VisibleLines = 1 Then

            Line1.Visible = True

        ElseIf VisibleLines = 2 Then

            Line1.Visible = True

            Line2.Visible = True

        ElseIf VisibleLines = 3 Then

            Line1.Visible = True

            Line2.Visible = True

            Line3.Visible = True

        ElseIf VisibleLines = 4 Then

            Line1.Visible = True

            Line2.Visible = True

            Line3.Visible = True

            Line4.Visible = True

        ElseIf VisibleLines = 5 Then

            Line1.Visible = True

            Line2.Visible = True

            Line3.Visible = True

            Line4.Visible = True

            Line5.Visible = True

        ElseIf VisibleLines = 6 Then

            Line1.Visible = True

            Line2.Visible = True

            Line3.Visible = True

            Line4.Visible = True

            Line5.Visible = True

            Line6.Visible = True

        End If

    End Sub

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Letter1.Click

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        clearLetters()

        hideCharachterLength()

End Sub

    Private Sub PlayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlayButton.Click

        Char1 = “”

        Char2 = “”

        Char3 = “”

        Char4 = “”

        Char5 = “”

        Char6 = “”

        DrawHanger()

        Dim number As Integer

        number = Int(Rnd() * ListBox1.Items.Count - 1) + 1

        Word = ListBox1.Items(number)

        WordLength = Word.Length

        ShowLength(Word.Length)

        clearLetters()

        AssignLetters()

        TextBox1.Focus()

        RightTrys = 0

        WrongTrys = 0

        WrongGuessesLabel.Text = “Wrong Guesses: “

        GameStarted = True

        PlayButton.Visible = False

    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

        If GameStarted = True Then

            If TextBox1.Text = “” Then

            Else

                If TextBox1.Text.ToUpper.Contains(Char1) And Letter1.Text = “” And Char1 “” Then

                    Letter1.Text = Char1

                    RightTrys += 1

                    CheckForWinning()

                ElseIf TextBox1.Text.ToUpper.Contains(Char2) And Letter2.Text = “” And Char2 “” Then

                    Letter2.Text = Char2

                    RightTrys += 1

                    CheckForWinning()

                ElseIf TextBox1.Text.ToUpper.Contains(Char3) And Letter3.Text = “” And Char3 “” Then

                    Letter3.Text = Char3

                    RightTrys += 1

                    CheckForWinning()

                ElseIf TextBox1.Text.ToUpper.Contains(Char4) And Letter4.Text = “” And Char4 “” Then

                    Letter4.Text = Char4

                    RightTrys += 1

                    CheckForWinning()

                ElseIf TextBox1.Text.ToUpper.Contains(Char5) And Letter5.Text = “” And Char5 “” Then

                    Letter5.Text = Char5

                    RightTrys += 1

                    CheckForWinning()

                ElseIf TextBox1.Text.ToUpper.Contains(Char6) And Letter6.Text = “” And Char6 “” Then

                    Letter6.Text = Char6

                    RightTrys += 1

                    CheckForWinning()

                Else

                    WrongTrys += 1

                    StartDrawingWhenLosing(WrongTrys)

                    WrongGuessesLabel.Text = WrongGuessesLabel.Text & ” ” & TextBox1.Text

                End If

            End If

        Else

       End If

        TextBox1.Text = “”

    End Sub

End Class

Change the following properties of the created form:

BackColor:  0, 192, 0

FormBorderStyle: FixedSingle

MinimizeBox: False

MaximiseBox: False

ShowIcon: False

Size: 440, 550

Text: Hangman

Add a Button to your form (Open Toolbox on the left side, or click on Ctrl+Alt+X if you don’t see it, then drag a button to the form).

Change its text to “Play”, its font to 18 ponits, its name to PlayButton and resize it to look similar to the following image:

Now add from 6 Labels from Toolbox to the form:

Change their font properties to 18 points, and their name to Letter1Letter2Letter3, etc…, their text to A and place them on the form like the following image:

Now add a Button to the form. This button will be used as the letters underline, so change the following properties of the button:

Enabled: False

FlatStyle: Flat

Size: 48, 2

Now make 5 copies of the button you just created and place them on the form

Change the buttons’ name properties to Line1Line2Line3, etc…

Now add a label to the bottom of the form and change its text to Wrong Guesses

There are two more controls needed to be added (a textbox and a listbox), but these two controls shouldn’t be visible to the user. You can make a control not visible to the user by changing its Visible property to False; however that doesn’t work in this case. Therefore, I will add a listbox and the textbox and place them outside the form boundary. Before I do that, I want to fill the listbox with words that will be used by the program to make the user guess them:Note that the words I use in this tutorial are less than 7 character long.One more thing needed to be done before hiding the textbox and the listbox is adjusting the TabIndex and TabStop propertiesChange Textbox1 TabIndexproperty to 0 and make sure its TabStop at True

Then change all other controls TabStop property to False . The reason we disable TabStopproperty from all other controls because we want to make sure that Textbox1 is always focusedand ready to receive keystrokes from the keyboard to be processed.

Mar 25. 0 Notes.