如何合并两个多行文本框 [英] How to merge two multiline textboxes
问题描述
我需要在vb net中合并两个多行文本框,如下所示:
textbox1:
一个
b
c
d
I need to combine two multi-line textboxes in vb net, like this:
textbox1:
a
b
c
d
textbox2:
1
2
3
4
textbox2:
1
2
3
4
textbox3:
a1
b2
c3
d4
只是一个带有三个文本框的表单.还有一个按钮,用于在t3中合并/组合/连接t1和t2中的每个值.
textbox3:
a1
b2
c3
d4
Just a form with three textboxes. And a button to merge/combine/concatenate each value from t1 and t2, in t3.
我的尝试之一:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
For Each line In TextBox1.Lines
For Each linex In TextBox2.Lines
Me.TextBox3.Text += line & linex
Me.TextBox3.Text += Environment.NewLine
Next
Next
End Sub
但是两个(a1,a2,a3,b1,b2,b3 ...)所取的行(lines = linex)的结果组合
but result combination of lines (lines=linex) taken by two (a1,a2,a3,b1,b2,b3...)
推荐答案
您可能有很多方法可以执行此操作.我在下面显示了一个,但您将假设第二个文本框包含与文本框1相同的行数.它不包含任何验证,但可以满足您的要求.
There are probably many ways you could do this. I have shown you one below but you would be assuming that textbox two contains the same amount of lines as textbox 1. It doesnt contain any validation but would do what you are asking.
查看评论以了解正在发生的事情.
See the comments to understand what is happening.
'Declare empty string for concatinating the text used in textbox 3
Dim lsText As String = String.Empty
'Loop for the count of lines in the textbox starting at an index of 0 for pulling data out
For i As Integer = 0 To TextBox1.Lines.Count - 1
'Check if lsText has already been assigned a value
If lsText = String.Empty Then
'If is has not then you know its the first so dont need a carriage return line feed simply take both values at that index
lsText = TextBox1.Lines(i) & TextBox2.Lines(i)
Else
'Otherwise you want the new value on a new line
lsText = lsText & vbCrLf & TextBox1.Lines(i) & TextBox2.Lines(i)
End If
Next
'Set the textbox text to the finished concatination
TextBox3.Text = lsText
这篇关于如何合并两个多行文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!