如何使此标签在每种情况下仅显示不同的字符串值

时间:2019-05-12 14:14:36

标签: vb.net

看看下面的简单代码。假设以不同的大小写显示字符串值,但是输出仅是第一个:“您的电子邮件是什么”。拜托,我需要解释。

目标是在表单上的下一个按钮上单击事件时更改标签文本。

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

我希望表单的输出在每种情况下都是不同的字符串值,但我一直只收到第一个字符串值。

1 个答案:

答案 0 :(得分:0)

您可以将位置存储在表单上,​​然后在放置每个Label和TextBox配对时增加位置:

    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