1.11M Members

Referring to separate lines in a multiline textbox



I have a textbox called



It is a multiline textbox, and I want to be able to enter different pieces of information (info1, info2, info3) into each line. I would really appreciate it if somebody could show me how to do this.



Do you just need to add values on separate lines, as: txtCustomerRecord.text = "info1" & vbNewLine & "info2" & vbNewLine & "info3" ?
.Or do you want to update a selected.line with a value?


Sorry, I wasn't very clear. I would like to be able to update a selected line with a value from a database, for example.


With TextBox1
            Dim arLines() As String = .Lines '// get all lines into a String.Array.
            arLines(2) = "line.3 has been updated" '// update selected line.
            .Lines = arLines '// add lines back to TextBox.
        End With

One way to do it would be

Dim lines() As String = TextBox1.Lines
lines(3) = "this is actually line 4"
TextBox1.Text = Join(lines, vbCrLf)

Of course, you would get an error if you didn't have enough existing lines. n alternative would be to use a listbox instead and do

ListBox1.Items(3) = "this is line 4"

directly. Same caveat as above.


Reverend Jim, you must either be getting old or have a slow p.c., cause I just beat you by a few seconds.:D


Thanks for both of your suggestions. I will try them both when I'm on a computer again, and get back to you :) (I'm posting from my phone)


Had to shoo the cat off the keyboard.


>>(I'm posting from my phone)
No vb.net on phone?:'(
>>Had to shoo the cat off the keyboard.
I ate my cat and keyboard and I still beat you by a few seconds, HAH!!!


Easy for you. You're in Florida where it's warm. I'm in Manitoba, in winter. Everything is slower here ;)


I'm in London lol. The internet makes the world such a small place...where's Manitoba?


Dead centre of Canada (at least east-west centre)


Oh, right. Brr.


I've attempted both methods, and got the came error:

Index was outside the bounds of the array.

Help please!


EDIT: I've managed to fix it by changing

Dim arrayLines() As String = .Lines


Dim arrayLines(3) As String



When you count lines or anything by using .Count or .Length, it starts at 1 then 2,3,4,etc.
When you use an Index, it starts at 0 Not 1, and it goes as 0,1,2,3,etc.
.this is the reason you might have/will notice the "-1"'s when working with .Items and Indexes.

You should always check if the line you would like to edit is there for you to edit.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        changeLineInCoolTextBox(0, ">>>>>>>>>") '// first line.
        changeLineInCoolTextBox(TextBox1.Lines.Length - 1, "<<<<<<<<<") '// last line.

    End Sub
    Private Sub changeLineInCoolTextBox(ByVal iCoolLineIndex As Integer, ByVal selContentToReplaceCoolLineWith As String)
        If Not iCoolLineIndex > TextBox1.Lines.Length - 1 Then '// check if the line you would like to replace exists in TextBox.
            Dim lines() As String = TextBox1.Lines
            lines(iCoolLineIndex) = selContentToReplaceCoolLineWith
            TextBox1.Text = Join(lines, vbCrLf)
            MsgBox("index was outside the bounds of the cool array, since it is greater(in this case) than the TextBox.Lines", MsgBoxStyle.Critical)
        End If
    End Sub

Thanks for that :)

Question Answered as of 2 Years Ago by codeorder and Reverend Jim
This question has already been solved: Start a new discussion instead
Start New Discussion
View similar articles that have also been tagged: