How do I add a newline to a windows-forms TextBox?

2019-02-11 12:01发布

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.

13条回答
疯言疯语
2楼-- · 2019-02-11 12:37
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.

查看更多
看我几分像从前
3楼-- · 2019-02-11 12:38

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.

查看更多
老娘就宠你
4楼-- · 2019-02-11 12:42

Try something like

"Line 1" & Environment.NewLine & "Line 2"
查看更多
forever°为你锁心
5楼-- · 2019-02-11 12:42

Have you tried something like:

textbox.text = "text" & system.environment.newline & "some more text"

查看更多
够拽才男人
6楼-- · 2019-02-11 12:42

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
查看更多
Lonely孤独者°
7楼-- · 2019-02-11 12:43

You can also use vbNewLine Object as in

MessageLabel.Text = "The Sales tax was:" & Format(douSales_tax, "Currency") & "." & vbNewLine & "The sale person: " & mstrSalesPerson
查看更多
登录 后发表回答