如何合并两个多行文本框

时间:2013-04-18 11:10:46

标签: vb.net textbox multiline

我需要在vb net中组合两个多行文本框,如下所示:
TextBox1的:
一个
b
ç
d

TextBox2中:
1
2
3
4

textbox3:
A1
B2
C3
D4
只是一个有三个文本框的表单。还有一个按钮,用于在t3中合并/组合/连接t1和t2中的每个值。

我的一次尝试:

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

但结果组合(line = linex)取两个(a1,a2,a3,b1,b2,b3 ......)

2 个答案:

答案 0 :(得分:0)

你可能有很多方法可以做到这一点。我在下面向您展示了一个,但您会假设文本框2包含与文本框1相同的行数。它不包含任何验证但会按照您的要求进行操作。

查看评论以了解正在发生的事情。

'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

答案 1 :(得分:0)

我会在一个循环中使用一个整数作为计数器,它会从count中提取每个字母,并且每次都会增加计数。

你要做的事情很简单,所以我不会提供任何代码 - 你不会以这种方式有效地学习。

只需知道您需要过滤换行符,知道每个文本框中有多少'char',并使用循环。 或许多其他方式,但我认为我所暗示的很容易,应该是你已经证明的5行代码。

祝你好运。继续发布您正在尝试的内容,如果我觉得您正在尝试,我会给予帮助。虽然现在我要睡觉了。

相关问题