我正在尝试向 VB.net 中的 TextBox 组件添加一行文本,但我终究无法弄清楚如何强制换行。现在它只是增加了我已经拥有的东西,这并不好。
我试过复制实际的换行符,没有用。我试过 AppendText(),没用。
我到底该怎么做?它已经是多线了。
Environment.NewLine
:
获取为此环境定义的换行符字符串。
这样的事情应该有效:
textBox.AppendText("your new text" & Environment.NewLine)
尝试类似的东西
"Line 1" & Environment.NewLine & "Line 2"
您是否将 AcceptsReturn 属性设置为 true?
你有没有试过这样的事情:
textbox.text = "text" & system.environment.newline & "some more text"
首先,您必须将
MultiLine
的TextBox
属性设置为true
,以便它支持多行。
然后你只需使用
Environment.NewLine
来获取换行符组合。
尝试
vbCrLf
.
例如:
TextBox1.text = "line_one" & vbCrLf & "line_two"
TextBox2.Text = "Line 1" & Environment.NewLine & "Line 2"
或
TextBox2.Text = "Line 1"
TextBox2.Text += Environment.NewLine
TextBox2.Text += "Line 2"
这就是它的完成方式。
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
您也可以使用
vbNewLine
对象,如
MessageLabel.Text = "The Sales tax was:" & Format(douSales_tax, "Currency") & "." & vbNewLine & "The sale person: " & mstrSalesPerson
从 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
确保你的文本框设置为多行然后你不需要任何额外的暗淡 vbnewline 会工作得很好
richtextbox 还有一个“Lines”属性,它是一个字符串数组。此数组中的每个项目都以隐式换行符结尾,并将显示在自己的行上。
如果您的文本是静态的或具有初始值,并且您在 Visual Studio 中使用设计器,则可以直接在其中直接添加行。
使用下面的文字!
TextBox1.Text = "This is a test"
TextBox1.Text = TextBox1.Text & ControlChars.Newline & "This is line 2"
controlchars.Newline
会自动将"This is line 2"
放到下一行。
你可以试试这个:
“这是1号线 这是2号线”
另一种方法是使用
vbCrLf
,这是一个 VB.NET 字符返回。
console.writeline("This is the first line," & vbCrLf & "...and this is the second line.")
Output:
This is the first line,
...and this is the second line.
我也在想办法解决这个问题。你能不能只用新行输出你专门写的东西?我正在尝试在一个输出控件中显示剧院座位、名称和成本。现在我有 3 个标签,但我需要在一个控件中。我试图在文本框中执行此操作,但我无法获得它可以工作,有多个在线文章和在这里。任何人都可以帮忙吗?