Search code examples
vb.netline

vb.net select specific lines, from one line to another line


I want write this code but it doesn't work for me

    Dim x

    Dim y

    x = TextBox1.Lines(1 - 5)


    y = TextBox1.Lines(6 - 10)


    TextBox2.Text = x
    TextBox3.Text = y

I want to take value from textbox.. x from line 1 to line 5 , y from line 6 to line 10


Solution

  • Make sure there are 10 lines in there, otherwise add some validation / error handling

    Dim x = TextBox1.Lines.Take(5)
    Dim y = TextBox1.Lines.Skip(5).Take(5)
    TextBox2.Text = String.Join(Environment.NewLine, x)
    TextBox3.Text = String.Join(Environment.NewLine, y)
    

    You don't even need x and y according to your edit. Simply this

    TextBox2.Text = String.Join(Environment.NewLine, TextBox1.Lines.Take(5))
    TextBox3.Text = String.Join(Environment.NewLine, TextBox1.Lines.Skip(5).Take(5))