Reputation: 51
If I would enter 5 to textbox, the answer would be like this
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
then if i would enter 10 or more the answer would be
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
my code:
Private Sub CmdOk_Click()
Dim a, b, c, d, z As Integer
z = TxtInput.Text
For a = 1 To z Step 1
List1.AddItem a
For b = 2 To z Step 1
List1.AddItem a & " " & b
For c = 3 To z step 1
List1.AddItem a & " " & b & " " & c
For d = 4 To z Step 1
List1.AddItem a & " " & b & " " & c & " " & d
Next d
Next c
Next b
Next a
End Sub
Upvotes: 2
Views: 2251
Reputation: 309
This works. Try this.
List1.Items.Clear()
Dim i, j, z As Integer
Dim x, a As String
z = TxtInput.Text
For i = 1 To z
For j = 1 To i
x = Convert.ToString(j)
a = a & " " & x
Next
List1.Items.Add(a)
a = ""
Next
Upvotes: 0
Reputation: 15772
In one line (edited for clarity)
List1.Items.AddRange(
Enumerable.Range(1, Integer.Parse(Me.txtInput.Text)).
Select(Of String)(
Function(i)
Return Enumerable.Range(2, i - 1).
Select(Of String)(Function(i1) i1.ToString()).
Aggregate(Of String)("1", Function(s1, s2) s1 & " " & s2)
End Function).ToArray())
As a side-note, this approach is about twice as fast as the other answer, important if the number gets large.
Upvotes: 0
Reputation: 1082
Why are you making it so complicated?
That is suppose to work :
Private Sub CmdOk_Click()
Dim z as String = TxtInput.Text
Dim ListTemporary as String = "1"
For i = 2 To Integer.parse(z) Step 1
ListTemporary = ListTemporary & " " & i.ToString()
List1.AddItem ListTemporary
Next
End Sub
Upvotes: 2
Reputation: 1326
Private Sub CmdOk_Click()
ListBox1.Items.Clear() ' clear the list before processing
Dim s As String = "" ' initialize a string variable
For i As Integer = 1 To CInt(TextBox1.Text) ' i loop for row counting
s = "" ' in each itetration clear the s value
For j As Integer = 1 To i ' for format the string to display in each index
s = s & j & " "
Next
ListBox1.Items.Add(s) ' add each item in row index
Next
End Sub
Upvotes: 1