2

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

Output of my code

3
  • 3
    did you have a question? Commented Jul 29, 2014 at 17:18
  • @Plutonix His problem is probably that it isn't displaying properly on screen. But yes, he didn't write any question. Commented Jul 29, 2014 at 17:25
  • I had posted a simplest method below Commented Jul 30, 2014 at 4:14

4 Answers 4

2

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
Sign up to request clarification or add additional context in comments.

Comments

1
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

Comments

0

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.

3 Comments

This isn't codegolf.stackexchange.com. But nice one anyhow. (Good luck debugging that afterward though...)
@Sifu Thanks. Mine's twice as fast as yours.
I don't doubt that yours is much much much faster than mine. It's like comparing the size of Jquery.js and Jquery.min.js, sure the minimized version is lighter and faster, but can you understand anything in it? All I was saying, is that it is much harder to understand what is going on in your answer, even though it is much more efficient and faster.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.