1.11M Members

Referring to separate lines in a multiline textbox

 
0
 

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

 
0
 

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?

 
0
 

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

 
1
 
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
 
1
 

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.

 
0
 

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

 
0
 

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)

 
0
 

Had to shoo the cat off the keyboard.

 
0
 

>>(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!!!

 
0
 

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

 
0
 

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

 
0
 
 
0
 

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

 
0
 

Oh, right. Brr.

 
0
 

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

 
0
 

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
 
0
 

Thanks for that :)

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