0

我想知道如何在 vb.net 中读取和转换 Richtextbox 的不同行,例如,如果这些是 RichTextBox 的行:

    Hello
    Hi

我怎样才能把它转换成类似的东西:

    Yo(Hello)
    Yo(Hi)

并将结果放在第二个富文本框中?

4

3 回答 3

2

RichtextBox 有一个lines属性:

    Dim rtb_in As New RichTextBox
    Dim rtb_out As New RichTextBox
    For Each line In rtb_in.Lines
        rtb_out.AppendText(String.Format("Foo({0})", line))
    Next

查看 MSDN 以了解您使用的课​​程总是一个好主意......

于 2012-11-15T17:05:21.047 回答
0

您可以尝试在新行上拆分并修改结果:

Dim box1Lines as String() = richTextBox1.Text.Split(new String() { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)

Dim newLines as String = ""

For Each line As String in box1Lines

    newLines += "Yo(" & line & ")" & Environment.NewLine

Next

richTextBox2.Text = newLines
于 2012-11-15T17:04:08.877 回答
0

可能你应该用String.Join它来完成它。一个one line解决方案是:

rtbOut.Lines = ("Yo(" & String.Join(")" & Environment.NewLine & "Yo(", rtbIN.Lines) & ")").Split(Environment.NewLine.ToCharArray, StringSplitOptions.RemoveEmptyEntries)

这是将修改后的行分配给第二个的完整代码RichTextBox

Dim rtbIN As New RichTextBox
Dim rtbOut As New RichTextBox
rtbIN.Lines = New String() {"Hello ", "Hi"}
rtbOut.Lines = ("Yo(" & String.Join(")" & Environment.NewLine & "Yo(", rtbIN.Lines) & ")").Split(Environment.NewLine.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
于 2012-11-16T12:16:42.460 回答