Question Do Until loop - adding text (as integer) to a textbox

whapster@

Member
Joined
Aug 12, 2009
Messages
7
Programming Experience
Beginner
HI All

I am trying to understand the do until loop when adding numbers along with a variable to a new line in a textbox, incrementing each time.

My current example is very simple but only returns the first value. I require the output to be incremented automatically, so I thought a do until loop would be best.

current code:

VB.NET:
Dim linenumber As String
        Dim lines As Integer
        Dim linestart As Integer
        Dim linesend As Integer
        linenumber = "Line number:"
        lines = 0
        linestart = lines + 1
        linesend = 10
        TextBox1.Text = (linenumber & linestart & (vbNewLine & linenumber & linesend))

The outcome is for me expected so far, all the above gives is Line number: 1 and Line number 10: but I am not understanding this loop.

I need to the output to be

Line number: 1
Line number: 2
Line number: 3

through to Line number 10

Any help?

Thanks
 

Attachments

  • Capture.JPG
    Capture.JPG
    19.1 KB · Views: 42
You need DO to say where the loop starts
Loop to say when to go back there
And Until or While to say when to stop going back.

VB.NET:
Dim linenumber As String 
Dim lines As Integer ' The line being processed.
Dim linestart As Integer ' The first line number
Dim linesend As Integer ' The last line number
linenumber = "Line number:"
lines = linestart - 1 ' Starts counting lines. Starts from 1 less than linestart, so that when you add 1 it becomes linestart


[B][B][B]Do ' Everything from here to loop will be repeated
lines += 1 ' Adds one to the line being processed
TextBox1.Text = textbox1.text & environment.newline & Linenumber & lines ' Adds the current line to the text box.
Loop until lines = linesend ' Goes back to the Do statement, unless you've done the last line.[/B][/B][FONT=Verdana]
[/FONT][/B]


However it is better to do counting loops with FOR.

VB.NET:
For L = 1 to linesend
TextBox1.Text = textbox1.text & environment.newline & Linenumber & lines ' Adds the current line to the text box.
Next L
 
thank you so much for the guidance, very simple after your clear instructions, this works a treat.

Currently I have the following


VB.NET:
 Dim linenumber As String
            Dim linenum As Integer
            Dim lines As Integer
            Dim linestart As Integer
            Dim linesend As Integer


            linenum = lines + 1
            linenumber = "Line: "
            lines = 0
            linestart = lines + 1
            linesend = 100


            For L = 1 To linesend
                RichTextBox2.Text = RichTextBox2.Text & vbNewLine & linenumber & L ' Adds the current line to the text box.
            Next L

Now I have the next conumdrum...

I need to read the lines of a textbox after loading a textfile and append the amount of lines to the integer 'linesend' that I can use in the above code to determine the value.

I then have a splitcontainer with 2 textboxes the left box will contain the line number code above and the 2nd textbox that contains the textfile I have loaded. The loading of the textfile works perfectly but I need to make sure the left textbox scrolls with the right text box.

any ideas?
 
Back
Top