Take a look at the simple code below. It is suppose to show string values at different case but the output is only the first one: "what is your email". Please, I need explanation.

The objective is to change the labll text upon click event on next button on the form.

Public Class Form4PassworRecovery
    Dim counter As Integer = 0
    Private Sub Button1Next_Click(sender As Object, e As EventArgs) Handles Button1Next.Click
        Label1Intro.Hide()

        Select Case counter
            Case 0
                Question("What is your Email?")
            Case 1
                Question("What is your favorite Hobby")
            Case 2
                Question("What is your minor")
        End Select
        counter += 1
        Answer()
    End Sub

    Sub Answer()
        Dim A As New TextBox
        A.Location = New Point(66.5, 120)
        A.ForeColor = Color.White
        A.BackColor = Color.FromArgb(153, 217, 255)
        A.Size = New Point(400, 29)
        GroupBox1.Controls.Add(A)
        A.Show()
    End Sub

    Sub Question(ByVal Question As String)
        Dim Q As New Label
        Q.Text = Question
        Q.Location = New Point(66.5, 90)
        Q.Size = New Point(400, 29)
        Q.ForeColor = Color.White
        Q.BackColor = Color.FromArgb(153, 217, 255)
        GroupBox1.Controls.Add(Q)
        Q.Show()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Button1.Text = "Cancel" Then
            Me.Hide()
            Me.Dispose()
        End If
    End Sub
End Class

Am expecting the output of the form to be different string value at each case but I kept receiving only the first string value.

1 Answers

0
Andrew Mortimer On Best Solutions

You can store the locations on your form and then increment them as you place each Label and TextBox pairing:

    Dim counter As Integer = 0
    Dim labelLocation As Point = New Point(5, 5)
    Dim textBoxLocation As Point = New Point(5, 30)

    Private Sub Button1Next_Click(sender As Object, e As EventArgs) Handles Button1Next.Click
        Label1Intro.Hide()

        Select Case counter
            Case 0
                Question("What is your Email?")
            Case 1
                Question("What is your favorite Hobby")
            Case 2
                Question("What is your minor")
        End Select
        counter += 1
        Answer()
    End Sub

    Sub Question(ByVal Question As String)

        Dim Q As New Label
        Q.Text = Question
        Q.Location = labelLocation

        Q.Size = New Size(400, 29)
        Q.ForeColor = Color.White
        Q.BackColor = Color.FromArgb(153, 217, 255)
        GroupBox1.Controls.Add(Q)
        Q.Show()

        textBoxLocation = New Point(labelLocation.X, labelLocation.Y + Q.Height + 7)
        labelLocation = New Point(labelLocation.X, textBoxLocation.Y + 7 + Q.Height)

    End Sub

    Sub Answer()

        Dim A As New TextBox
        A.Location = textBoxLocation

        A.ForeColor = Color.White
        A.BackColor = Color.FromArgb(153, 217, 255)
        A.Size = New Size(400, 29)
        GroupBox1.Controls.Add(A)
        A.Show()

    End Sub