Hi,

I have a textbox called

txtCustomerRecord.text

.

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.

Collin

Dani AI

Generated

Good tips from and . The error you hit comes from trying to write to a line index that does not exist yet. Declaring Dim arrayLines(3) As String only creates a new 4-element array and discards whatever was in the textbox; it does not solve the general case. Arrays are 0-based: if Lines.Length = 3, valid indexes are 0..2. Always check length and, when needed, pad with blank lines before replacing.

WinForms: update a specific line index (0-based) and grow the lines if needed.

Private Sub UpdateLine(tb As TextBox, lineIndex As Integer, newValue As String)
    Dim lines As List(Of String) = tb.Lines.ToList()
    While lines.Count <= lineIndex
        lines.Add(String.Empty)
    End While
    lines(lineIndex) = newValue
    tb.Lines = lines.ToArray()
End Sub

' Example: put a DB value on line 2 (third line)
UpdateLine(TextBox1, 2, dbValue)

WinForms: update the currently selected line (caret line).

Private Sub UpdateSelectedLine(tb As TextBox, newValue As String)
    Dim idx As Integer = tb.GetLineFromCharIndex(tb.SelectionStart)
    UpdateLine(tb, idx, newValue)
End Sub

ASP.NET WebForms note: there is no Lines property. Use Split/Join on Text.

Dim parts As List(Of String) =
    TextBox1.Text.Split(New String(){Environment.NewLine}, StringSplitOptions.None).ToList()

While parts.Count <= targetIndex
    parts.Add(String.Empty)
End While

parts(targetIndex) = dbValue
TextBox1.Text = String.Join(Environment.NewLine, parts)

If the data is truly structured (info1/info2/info3), consider binding those fields to a ListBox, GridView, or DataGridView instead of packing them into a free-form textbox. That makes updates as simple as Items(i) = value, as suggested, and avoids fragile string handling.

Recommended Answers

All 16 Replies

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.

Thanks

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!

Thanks

EDIT: I've managed to fix it by changing

Dim arrayLines() As String = .Lines

to

Dim arrayLines(3) As String

:D

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)
        Else
            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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.