Write in new line in rich text box. with vb

拜拜、爱过 提交于 2019-12-02 07:21:55

问题


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.


回答1:


I like to use vbCrLf constant:

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



回答2:


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

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




回答3:


You can use the vbCrLf constant:

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



回答4:


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...

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.



来源:https://stackoverflow.com/questions/15837041/write-in-new-line-in-rich-text-box-with-vb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!