我有2个文本机器人,第一个有几行。我想编辑文本框1中的每一行,将文本框2中的文本添加到每一行。
因此,如果textbox1有以下行:
i am
you are
he is
she is
并且文本框2包含文本:John
然后textbox1将成为
i am John
you are John
he is John
she is John
我试过了:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each strLine As String In TextBox1.Text.Split(vbNewLine)
strLine += " " + TextBox2.Text
Next
End Sub
但没有成功,有什么帮助?
答案 0 :(得分:2)
您可以直接修改strLine,因为它不会影响文本框值。您需要创建一个新变量,然后使用它来替换Textbox1.Text
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim newText as New String()
For Each strLine As String In TextBox1.Text.Split(vbNewLine)
newText &= strLine & " " & TextBox2.Text & Environment.NewLine
Next
Textbox1.Text = newText
End Sub
检查语法,因为我没有多少使用Vb.net。
答案 1 :(得分:0)
您也可以这种方式使用文本框的Lines属性。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim newText As New List(Of String)
For Each strLine As String In TextBox1.Lines
newText.Add(strLine & " " & TextBox2.Text)
Next
Textbox1.Lines = newText.ToArray
End Sub
请确保您不要尝试直接在线上工作。您必须处理新的集合,然后用新的集合替换lines属性。