MAX.RAY MAX.RAY - 4 months ago 13
Vb.net Question

How to add 10 numbers to a ListBox with less code?

I coded this. It's fine but can I write it with less code?

If D = 10 Then
ListBox3.Items.Add(1)
ListBox3.Items.Add(2)
ListBox3.Items.Add(3)
ListBox3.Items.Add(4)
ListBox3.Items.Add(5)
ListBox3.Items.Add(6)
ListBox3.Items.Add(7)
ListBox3.Items.Add(8)
ListBox3.Items.Add(9)
ListBox3.Items.Add(10)
End If


if you don't understand my question you can read the following example:

We can code

Dim A As Integer
Dim B As Integer
Dim C As Integer
Dim D As Integer


but we can code it in a more concise way like this

Dim A, B, C, D As Integer

Answer

I think you're looking for a For Loop:

    For i As Integer = 1 To 10
        ListBox3.Items.Add(i)
    Next