Referring to separate lines in a multiline textbox
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
collin_ola
Junior Poster in Training
55 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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?
codeorder
Postaholic
2,124 posts since Aug 2010
Reputation Points: 256
Solved Threads: 387
Skill Endorsements: 8
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
collin_ola
Junior Poster in Training
55 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
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
codeorder
Postaholic
2,124 posts since Aug 2010
Reputation Points: 256
Solved Threads: 387
Skill Endorsements: 8
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
Carpe per diem
3,591 posts since Aug 2010
Reputation Points: 561
Solved Threads: 445
Skill Endorsements: 32
Reverend Jim, you must either be getting old or have a slow p.c., cause I just beat you by a few seconds.:D
codeorder
Postaholic
2,124 posts since Aug 2010
Reputation Points: 256
Solved Threads: 387
Skill Endorsements: 8
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)
collin_ola
Junior Poster in Training
55 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Had to shoo the cat off the keyboard.
Reverend Jim
Carpe per diem
3,591 posts since Aug 2010
Reputation Points: 561
Solved Threads: 445
Skill Endorsements: 32
>>(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!!!
codeorder
Postaholic
2,124 posts since Aug 2010
Reputation Points: 256
Solved Threads: 387
Skill Endorsements: 8
Easy for you. You're in Florida where it's warm. I'm in Manitoba, in winter. Everything is slower here ;)
Reverend Jim
Carpe per diem
3,591 posts since Aug 2010
Reputation Points: 561
Solved Threads: 445
Skill Endorsements: 32
I'm in London lol. The internet makes the world such a small place...where's Manitoba?
collin_ola
Junior Poster in Training
55 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
codeorder
Postaholic
2,124 posts since Aug 2010
Reputation Points: 256
Solved Threads: 387
Skill Endorsements: 8
Dead centre of Canada (at least east-west centre)
Reverend Jim
Carpe per diem
3,591 posts since Aug 2010
Reputation Points: 561
Solved Threads: 445
Skill Endorsements: 32
collin_ola
Junior Poster in Training
55 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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
collin_ola
Junior Poster in Training
55 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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
codeorder
Postaholic
2,124 posts since Aug 2010
Reputation Points: 256
Solved Threads: 387
Skill Endorsements: 8
collin_ola
Junior Poster in Training
55 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by
codeorder
and
Reverend Jim