For-next loop problem

danlega2

New member
Joined
May 9, 2011
Messages
2
Programming Experience
Beginner
Hi VB.net gurus, I'm new in programming especially in vb.net and I have a problem displaying the following using for-next loops:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
...and so on...

initially I wrote this code

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim row As Integer
Dim col As Integer
Dim txtNumber As Integer = CInt(TextBox1.Text)
Dim txtOutput As String


For row = 1 To txtNumber Step 1

For col = row To (row + (row - 1))
txtOutput = txtOutput & " " & col
Next
txtOutput = txtOutput & vbCrLf

Next
Label1.Text = txtOutput

End Sub

the out put is this

1
2 3
3 4 5
4 5 6 7
5 6 7 8 9
... and so on...

Kindly help me on this one please to improve my knowledge in vb.net
 
Here's how I would do it:

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Label1.Text = getLines(CInt(TextBox1.Text))
End Sub

Private Function getLines(ByVal lines As Integer) As String
    Dim intNum As Integer = 1
    Dim strOutput As String = ""
    For row = 1 To lines
        For col = 1 To row
            strOutput &= " " & intNum
            intNum += 1
        Next
        strOutput &= vbCrLf
    Next
    Return strOutput
End Function


Hope this helps
-Josh
 
Many Thanks to you Joshdbr!

Thank you very much for helping me solve my studies in vb.net, I am more encourage to further study vb.net.


Thanks and God Bless...
 
Back
Top