vb.net 如何向 windows 窗体文本框添加换行符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1302804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:21:30  来源:igfitidea点击:

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

vb.netwinformsnewline

提问by Cyclone

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.

我正在尝试向 VB.net 中的 TextBox 组件添加一行文本,但我一生都无法弄清楚如何强制换行。现在它只是增加了我已经拥有的东西,这并不好。

I have tried copying the actual linebreaks, didn't work. I tried AppendText(), didn't work.

我试过复制实际的换行符,没有用。我试过 AppendText(),没有用。

How on earth do I do this? It is multiline already.

我到底该怎么做?已经是多行了。

回答by Andrew Hare

Try using Environment.NewLine:

尝试使用Environment.NewLine

Gets the newline string defined for this environment.

获取为此环境定义的换行符字符串。

Something like this ought to work:

这样的事情应该工作:

textBox.AppendText("your new text" & Environment.NewLine)

回答by JohannesH

Try something like

尝试类似的东西

"Line 1" & Environment.NewLine & "Line 2"

回答by shahkalpesh

Have you set AcceptsReturnproperty to true?

您是否将AcceptsReturn属性设置为 true?

回答by Kevin LaBranche

Have you tried something like:

您是否尝试过类似的事情:

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

textbox.text = "text" & system.environment.newline & "更多文本"

回答by Guffa

First you have to set the MultiLineproperty of the TextBoxto trueso that it supports multiple lines.

首先,你必须设定MultiLine的属性TextBox,以true使其支持多条线路。

Then you just use Environment.NewLineto get the newline character combination.

然后您只需使用Environment.NewLine来获取换行符组合。

回答by JeffK

Quickie test code for WinForms in VB:

VB中WinForms的快速测试代码:

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

回答by Hayden Madden

Took this from JeffKand made it a little more compact.

JeffK那里拿来这个,让它更紧凑一点。

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

回答by Thimo Braker

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.

这,就是这样做的。

回答by user2801499

Use the text below!

使用下面的文字!

TextBox1.Text = "This is a test"
TextBox1.Text = TextBox1.Text & ControlChars.Newline & "This is line 2"

The controlchars.Newlinewill automatically put "This is line 2"to the next line.

controlchars.Newline会自动把"This is line 2"下一行。

回答by TenTen Peter

You can also use vbNewLineObject as in

您还可以使用vbNewLineObject 作为

MessageLabel.Text = "The Sales tax was:" & Format(douSales_tax, "Currency") & "." & vbNewLine & "The sale person: " & mstrSalesPerson