-3

I need to combine two multi-line textboxes in vb net, like this:
textbox1:
a
b
c
d

textbox2:
1
2
3
4

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.

One of my attempts:

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

but result combination of lines (lines=linex) taken by two (a1,a2,a3,b1,b2,b3...)

4

2 回答 2

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
于 2013-04-18T13:26:31.050 回答
0

我会在循环中使用一个整数作为计数器,该循环将从每个计数值处提取字母,并每次增加计数。

您尝试做的事情非常简单,所以我不会提供任何代码 - 这样您不会有效地学习。

只要知道您需要过滤换行符,知道每个文本框中有多少个“字符”,并使用循环。或许多其他方式,但我认为我所暗示的很简单,应该是大约 5 行代码,您已经在某种程度上演示过。

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

于 2013-04-18T13:32:30.137 回答