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
54 posts since Jan 2012
Reputation Points: 10
Solved Threads: 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
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
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
54 posts since Jan 2012
Reputation Points: 10
Solved Threads: 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
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
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
Posting Shark
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
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
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
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
54 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Had to shoo the cat off the keyboard.
Reverend Jim
Posting Shark
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
>>(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
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
Easy for you. You're in Florida where it's warm. I'm in Manitoba, in winter. Everything is slower here ;)
Reverend Jim
Posting Shark
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
I'm in London lol. The internet makes the world such a small place...where's Manitoba?
collin_ola
Junior Poster in Training
54 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
Dead centre of Canada (at least east-west centre)
Reverend Jim
Posting Shark
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
collin_ola
Junior Poster in Training
54 posts since Jan 2012
Reputation Points: 10
Solved Threads: 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
54 posts since Jan 2012
Reputation Points: 10
Solved Threads: 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
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
collin_ola
Junior Poster in Training
54 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0