Question

Currently my code looks like this:

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

As you can see, I have added a variable and the loop runs fine, but I need help phrasing the code so whatever number the user inputs, is the number of lines of "Study!" displayed. I have tried using the string but also using the label with Environment.NewLine hoping it would add a new line every time the loop runs

Was it helpful?

Solution

You are setting the label text to strStudy each time. This doesn't add to what's there already, it just replaces it.

You need to add strStudy to the text that already exists, something like this:

' 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

OTHER TIPS

Instead of using a TextBox, use a ListBox to display the word Study! n -times. It is also good practice not to instruct the user to do something, but prevent it in code. Also note how intNumber has been changed to *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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top