user7500958
user7500958

Reputation:

Adding a new label at every loop iteration

I'm trying to make a hangman application, but for whatever reason I can not get the word you must "guess" to show up properly. As you can see in the loop, a new label is created at every iteration. The label's text property is set as a character of wordString, defined as wordLetter. Once all properties of the new label are set, it is added to the form, points are defined to draw a line underneath the label, and xAxis (used for placing the next label to the right of the previous) and i are iterated. The problem is, only the first label shows up, or "P" from the wordString "PROGRAM". I believe it's because every time I iterate the loop, a new label of the same name "wordLabel" is created, preventing new labels from being created. I'm not sure how to get around this; even if I used an array, I'd still have to make a new label at every iteration.

The result

Dim point1, point2 As Point

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim i As Integer

    Dim wordString As String = "PROGRAM"
    Dim wordLetter As String
    Dim xAxis As Integer

    Do While i < wordString.Length
        Dim wordLabel As New Label

        wordLetter = wordString.Chars(i)

        wordLabel.Font = New Font("Comic Sans MS", 25)
        wordLabel.AutoSize = True
        wordLabel.Text = wordLetter
        wordLabel.BackColor = Color.Transparent
        wordLabel.Location = New System.Drawing.Point(xAxis + 7, 190)

        Me.Controls.Add(wordLabel)

        point1.X = Convert.ToInt32(wordLabel.Location.X) - 10
        point1.Y = Convert.ToInt32(wordLabel.Location.Y) + 40

        point2.X = Convert.ToInt32(wordLabel.Size.Width) - 13
        point2.Y = Convert.ToInt32(wordLabel.Location.Y) + 40

        xAxis += 3
        i += 1

    Loop
End Sub

Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint

    e.Graphics.DrawLine(Pens.Black, point1, point2)

End Sub

Upvotes: 0

Views: 921

Answers (1)

Andrew Mortimer
Andrew Mortimer

Reputation: 2370

This should work for you. It keeps the last left position of the previous label.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Try
        BuildHangman("PROGRAM")
    Catch ex As Exception
        MessageBox.Show(String.Concat("An error occurred: ", ex.Message))
    End Try
End Sub

Private Sub BuildHangman(wordString As String)

    Dim i As Integer
    Dim wordLetter As String
    Dim lastLeft As Integer

    Dim sensibleFont As New Font("Segoe UI", 25)

    Do While i < wordString.Length

        Dim wordLabel As New Label

        wordLetter = wordString.Chars(i)
        wordLabel.Font = sensibleFont
        wordLabel.AutoSize = True
        wordLabel.Text = wordLetter
        wordLabel.BackColor = Color.Transparent
        wordLabel.Location = New System.Drawing.Point(lastLeft + 7, 190)

        Me.Controls.Add(wordLabel)

        lastLeft = wordLabel.Left + wordLabel.Width

        i += 1

    Loop

End Sub

Upvotes: 1

Related Questions