Dibbie Games
Dibbie Games

Reputation: 13

Making text go to a new line when in a while loop

I have a richTextbox, and a while loop, x = 0, and every time this loop runs, x += 1 till it reaches a certain value.

Heres what I want to happen:
while x <> 10 then it will say item 0 +1 on a new line, and then item 1 + 1 on the line under it, etc, so you will see all 10 values after.

What happens is, it will change that line into the new value.

My question is: How do I make it put the words on a new line instead?

Here is my code:

While readListItem <> PaymentList.Items.Count
    If readListItem > PaymentList.Items.Count Then
        readListItem = 0
        Exit While
    End If
    readListItem += 1
    MessageBox.Show(readListItem)
    resultBox.Text = vbNewLine + PaymentList.Items.Item(readListItem) + " costs " + enteredCost + "." + vbNewLine
End While

readListItem is "x", and that is being iterated by 1 every time the loop runs PaymentList is my listbox containing an unknown value (the user sets the number of items in this list)

The If if there because, after x = lets say 10, it would add another to x (so now it = 11) and try to print out the 11th item, as it doesnt exist, it crashes. This is to attempt to prevent that. I didnt want to go with a try catch

Upvotes: 0

Views: 850

Answers (1)

SysDragon
SysDragon

Reputation: 9888

Try adding the new value instead of replacing the entire value:

resultBox.Text &= Environment.NewLine() & PaymentList.Items.Item(readListItem) + " costs " + enteredCost + "."

The expression &= is equivalent, in this case, to resultBox.Text = resultBox.Text & "...".

And once advice, you can simplify your loop by using this condition:

While readListItem < PaymentList.Items.Count
    '...
End While

Upvotes: 2

Related Questions