缩短交叉口功能VB Net

时间:2018-12-28 18:06:03

标签: vb.net

如何使这段代码更短?我有60个文本框,每次都必须重写代码,如果我错了,就必须获取所有代码。如下所示,所有值都必须替换为+1。在第一个代码Draw1处,我忘记了它的名字分别为FirstBoxList1和SecondListBox1,都带有1。在这里,我编写了前三个代码,但一直持续到Draw60。 txtDrawsr-是比较的文本框,所以1到2,第一个画,2到3画2,3到4画3。

    ' Draw 1
    Dim firstBoxList = txtDrawsr1.Text.Split(",").ToList()
    Dim secondBoxList = txtDrawsr2.Text.Split(",").ToList()
    Dim intersectionList = firstBoxList.Intersect(secondBoxList)
    For Each str As String In intersectionList
        txtIntDraw1.Text = txtIntDraw1.Text & str & ","
    Next
    txtIntCount1.Text = intersectionList.Count()
    Dim notRepeatedCharacter = firstBoxList.Union(secondBoxList).ToList
    notRepeatedCharacter.RemoveAll(Function(x) intersectionList.Contains(x))
    For Each str As String In notRepeatedCharacter
        txtNonIntDraw1.Text = txtNonIntDraw1.Text & str & ","
    Next
    txtNonIntCount1.Text = notRepeatedCharacter.Count()
    firstBoxList.RemoveAll(Function(x) secondBoxList.Contains(x))
    For Each str As String In firstBoxList
        txtIntNonI1.Text = txtIntNonI1.Text & str & ","
    Next
    txtIntNonCountI1.Text = firstBoxList.Count
    'Draw: 2
    Dim firstBoxList2 = txtDrawsr2.Text.Split(",").ToList()
    Dim secondBoxList2 = txtDrawsr3.Text.Split(",").ToList()
    Dim intersectionList2 = firstBoxList2.Intersect(secondBoxList2)
    For Each str As String In intersectionList2
        txtIntDraw2.Text = txtIntDraw2.Text & str & ","
    Next
    txtIntCount2.Text = intersectionList2.Count()
    Dim notRepeatedCharacter2 = firstBoxList2.Union(secondBoxList2).ToList
    notRepeatedCharacter2.RemoveAll(Function(x) intersectionList2.Contains(x))
    For Each str As String In notRepeatedCharacter2
        txtNonIntDraw2.Text = txtNonIntDraw2.Text & str & ","
    Next
    txtNonIntCount2.Text = notRepeatedCharacter2.Count()
    firstBoxList2.RemoveAll(Function(x) secondBoxList2.Contains(x))
    For Each str As String In firstBoxList2
        txtIntNonI2.Text = txtIntNonI2.Text & str & ","
    Next
    txtIntNonCountI2.Text = firstBoxList2.Count
    'Draw 3
    Dim firstBoxList3 = txtDrawsr3.Text.Split(",").ToList()
    Dim secondBoxList3 = txtDrawsr4.Text.Split(",").ToList()
    Dim intersectionList3 = firstBoxList3.Intersect(secondBoxList3)
    For Each str As String In intersectionList3
        txtIntDraw3.Text = txtIntDraw3.Text & str & ","
    Next
    txtIntCount3.Text = intersectionList3.Count()
    Dim notRepeatedCharacter3 = firstBoxList3.Union(secondBoxList3).ToList
    notRepeatedCharacter3.RemoveAll(Function(x) intersectionList3.Contains(x))
    For Each str As String In notRepeatedCharacter3
        txtNonIntDraw3.Text = txtNonIntDraw3.Text & str & ","
    Next
    txtNonIntCount3.Text = notRepeatedCharacter3.Count()
    firstBoxList3.RemoveAll(Function(x) secondBoxList3.Contains(x))
    For Each str As String In firstBoxList3
        txtIntNonI3.Text = txtIntNonI3.Text & str & ","
    Next
    txtIntNonCountI3.Text = firstBoxList3.Count

1 个答案:

答案 0 :(得分:-2)

不确定为什么您需要那么多文本框,但类似的东西应该可以工作:

    For x = 1 To 60

        Dim firstBoxList = CType(Me.Controls("txtDrawsr" & x), TextBox).Text.Split(CChar(",")).ToList()
        Dim secondBoxList = CType(Me.Controls("txtDrawsr" & x + 1), TextBox).Text.Split(CChar(",")).ToList()

        Dim intersectionList = firstBoxList.Intersect(secondBoxList)
        CType(Me.Controls("txtIntDraw" & x), TextBox).Text = String.Join(",", intersectionList)
        txtIntCount1.Text = CStr(intersectionList.Count())

        Dim notRepeatedCharacter = firstBoxList.Union(secondBoxList).ToList
        notRepeatedCharacter.RemoveAll(Function(a) intersectionList.Contains(a))
        CType(Me.Controls("txtNonIntDraw" & x), TextBox).Text = String.Join(",", notRepeatedCharacter)

        txtNonIntCount1.Text = CStr(notRepeatedCharacter.Count())
        firstBoxList.RemoveAll(Function(a) secondBoxList.Contains(a))

        CType(Me.Controls("txtIntNonI" & x), TextBox).Text = String.Join(",", firstBoxList)
        CType(Me.Controls("txtIntNonCountI" & x), TextBox).Text = CStr(firstBoxList.Count)

    Next