下一个visual basic的标签循环

时间:2014-04-09 21:44:32

标签: vb.net for-loop label newline

目前我的代码如下:

Private Sub btnMotivate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMotivate.Click
    Dim intNumber As Integer
    Dim strStudy As String = "Study!"

    intNumber = InputBox("How many times do you want to be motivated? Please use numbers!")
    If intNumber < 1 Then
        MessageBox.Show("Please use a number from 1 - 10!")
    End If
    If intNumber > 10 Then
        MessageBox.Show("Please use a number from 1 - 10!")
    End If

>    For intCounter = 1 To intNumber Step 1
        lblMotivation.Text = strStudy
>    Next

End Sub

正如你所看到的,我已经添加了一个变量并且循环运行正常,但我需要帮助来填写代码,因此无论用户输入什么数字,都是“Study!”的行数。显示。我已经尝试使用字符串,但也使用带有Environment.NewLine的标签,希望每次循环运行时都会添加一个新行

2 个答案:

答案 0 :(得分:0)

您每次都将标签文字设置为strStudy。这并没有增加它已经存在的东西,只是替换它。

您需要将strStudy添加到已存在的文本中,如下所示:

' start with an empty string
lblMotivation.Text = ""
For intCounter = 1 To intNumber Step 1
    ' append the study string and a newline
    lblMotivation.Text = lblMotivation.Text & strStudy & Environment.NewLine
Next

答案 1 :(得分:0)

使用ListBox显示单词Study,而不是使用TextBox! n次。最好不要指示用户做某事,但要在代码中阻止它。另请注意intNumber如何更改为* inp * Number As Object

Private Sub btnMotivate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMotivate.Click
    Dim inpNumber As Object
    Dim strStudy As String = "Study!"
    Dim intCounter As Integer 
    inpNumber = InputBox("How many times do you want to be motivated?")
    If Not IsNumeric(inpNumber) Then
        MessageBox.Show("Please enter a number!")
    ElseIf inpNumber < 1 OrElse inpNumber > 10 Then
        MessageBox.Show("Please enter a number from 1 - 10!")
    Else
        ListBox1.Items.Clear()
        For intCounter = 1 To inpNumber Step 1
            ListBox1.Items.Add(strStudy)
        Next
    End If

End Sub