Write text on specific line in a textbox
I am trying to write text on a specific line in a textbox for example on the forth line)
I've tried :
TextBox1.Lines = TextBox1.Lines.Length
but get this error:
Value of type 'Integer' cannot be converted to '1-dimensional array of String'.
I assume this is beacause TextBox1.Lines is an array of all the text in every line in the textbox.
Multiline is set to true.
How to fix this?
Re: Write text on specific line in a textbox
vb.net Code:
Dim lines As String() = myTextBox.Lines
lines(3) = myString
myTextBox.Lines = lines
Re: Write text on specific line in a textbox
Thanks for the reply,
I get this error:
System.IndexOutOfRangeException was unhandled
Message="Index was outside the bounds of the array."
The textbox where I am trying to write text to is empty.
vb.net Code:
Dim lines As String() = TextBox1.Lines
lines(0) = "test"
TextBox1.Lines = lines
Re: Write text on specific line in a textbox
Well, that's kind of important information. How can you write to the fourth line of a TextBox that has no lines in it? There has to be at least four lines for there to be a fourth line.
If you want to add a new line to a TextBox you can do this:
vb.net Code:
myTextBox.AppendText(Environment.NewLine)
You can also assign any String array you like to the Lines property of a TextBox to overwrite all existing text. The array doesn't have to have come from the Lines property in the first place:
vb.net Code:
Dim lines As String() = {"Line 1", "Line 2", "Line 3"}
myTextBox.Lines = lines