vb.net 在富文本框中写新行。用 vb

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15837041/
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-17 13:07:38  来源:igfitidea点击:

Write in new line in rich text box. with vb

.netvb.netsplitrichtextbox

提问by N4pster

Hi guys i want to know how to write every word in a phrase in a new line in a richtextbox lets say the phrase is this "Returns a string array that contains the substrings in this instance that are delimited"

嗨,伙计们,我想知道如何在富文本框中的新行中写入短语中的每个单词,可以说这个短语是“返回一个字符串数组,其中包含此实例中已分隔的子字符串”

and the code i'm working with is this

我正在使用的代码是这个

Dim words As String = TextBox1.Text
    Dim split As String() = words.Split(New [Char]() {" "c, CChar(vbTab)})

    For Each s As String In split
        If s.Trim() <> "" Then
            RichTextBox1.Text = (s)
        End If
    Next s

But with this one it only write the last word of the sentence. And what i want is to write all the words each in a new line of the richtextbox.

但是有了这个,它只写了句子的最后一个字。我想要的是将所有单词都写在 Richtextbox 的新行中。

采纳答案by George

I like to use vbCrLfconstant:

我喜欢使用vbCrLf常量:

RichTextBox1.Text = TextBox1.Text.Replace(" ", vbCrLf).Replace(vbTab, vbCrLf)

回答by APrough

Your code is fine except for one thing. You are setting the RichTextBox to the string everytime, so it keeps overwriting. You need to concatenate...

除了一件事之外,您的代码很好。您每次都将 RichTextBox 设置为字符串,因此它会不断覆盖。你需要连接...

Dim words As String = TextBox1.Text
Dim split As String() = words.Split(New [Char]() {" "c, CChar(vbTab)})

For Each s As String In split
    If s.Trim() <> "" Then
        RichTextBox1.Text &= (s)
    End If
Next s

Note that addition of the "&" on the line that writes to the RTB.

请注意,在写入 RTB 的行上添加了“&”。

回答by Doan Cuong

Dim words As String = TextBox1.Text
words.Replace(" ", ControlChars.Lf)
RichTextBox1.Text = words

You just need to replace " "by ControlChars.Lfaka New Line Character

你只需要替换" "ControlChars.LfakaNew Line Character

回答by Metaphor

You can use the vbCrLf constant:

您可以使用 vbCrLf 常量:

    For Each s As String In split
        If s.Trim() <> "" Then
            RichTextBox1.Text = (s) + vbCrLf
        End If
    Next s