I am trying to add a line of text to a TextBox component in VB.net, but I cannot figure out for the life of me how to force a new line. Right now it just adds onto what I have already, and that is not good.
I have tried copying the actual linebreaks, didn't work. I tried AppendText(), didn't work.
How on earth do I do this? It is multiline already.
Took this from JeffK and made it a little more compact.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Newline As String = System.Environment.NewLine
TextBox1.Text = "This is a test"
TextBox1.Text += Newline + "This is another test"
End Sub
Try something like
"Line 1" & Environment.NewLine & "Line 2"
First you have to set the MultiLine
property of the TextBox
to true
so that it supports multiple lines.
Then you just use Environment.NewLine
to get the newline character combination.
You can also use vbNewLine
Object as in
MessageLabel.Text = "The Sales tax was:" & Format(douSales_tax, "Currency") & "." & vbNewLine & "The sale person: " & mstrSalesPerson
Have you tried something like:
textbox.text = "text" & system.environment.newline & "some more text"
Use the text below!
TextBox1.Text = "This is a test"
TextBox1.Text = TextBox1.Text & ControlChars.Newline & "This is line 2"
The controlchars.Newline
will automatically put "This is line 2"
to the next line.
Have you set AcceptsReturn property to true?
TextBox2.Text = "Line 1" & Environment.NewLine & "Line 2"
or
TextBox2.Text = "Line 1"
TextBox2.Text += Environment.NewLine
TextBox2.Text += "Line 2"
This, is how it is done.
make sure you textbox is set for multiline then you wont need any extra dims vbnewline will work just fine
Try vbCrLf
.
For example:
TextBox1.text = "line_one" & vbCrLf & "line_two"
You can try this :
"This is line-1 \r\n This is line-2"
Quickie test code for WinForms in VB:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Newline As String
Newline = System.Environment.NewLine
TextBox1.Text = "This is a test"
TextBox1.Text = TextBox1.Text & Newline & "This is line 2"
End Sub
The richtextbox also has a "Lines" property that is an array of strings. Each item in this array ends in an implicit line break and will be displayed on its own line.
If your text is static or has an initial value and you are using the designer in Visual Studio you can simply add lines directly there.
Source: Stackoverflow.com