在两个或多个文本框VB Net

时间:2018-12-28 08:57:52

标签: vb.net

我想将Textbox1与TextBox2或文本框的文本框第1行与第二行进行比较,以向我显示另一个文本框中的现有字符,或显示重复多少个字符。我真的很喜欢学习,因此会有所帮助,因为我想学习...

 TextBox1.Text = 1,4,7,11,13,16,19,20,28,31,44,37,51,61,62,63,64,69,71,79,80
 TextBox2.Text = 1,5,7,10,13,16,26,20,28,31,44,37,51,72,73,74,69,71,79,80
 TextBox3.Text = Character Repeated: 1,7,13,16,20,28,31,44,37,51,69,71,79,80
 TextBox4.Text = Number of Character Repeated = 14
 TextBox5.Text = Number of Character which has not been repeated: 4,11,19,61,62,63,64 etc, you got to idea
 TextBox6.Text = Number of Character isn't Repeated: 7

以下是一些代码:但是我不知道如何正确应用它们。

代码1:显示重复的字符:

   ' Split string based on space
    TextBox1.Text = System.IO.File.ReadAllText(Mydpi.Text)
    TextBox2.Text = System.IO.File.ReadAllText(Mydpi.Text)
    TextBox4.Text = System.IO.File.ReadAllText(Mydpi.Text)
    For i As Integer = 0 To TextBox2.Lines.Count - 1
        Dim textsrtring As String = TextBox4.Lines(i)
        Dim words As String() = textsrtring.Split(New Char() {","c})
        Dim found As Boolean = False
        ' Use For Each loop over words
        Dim word As Integer
        For Each word In words
            TxtbValBeforeCompar.Text = TextBox1.Lines(i)
            CompareNumbers()
            If TextBox1.Lines(i).Contains(word) Then
                found = True
                Dim tempTextBox As TextBox = CType(Me.Controls("Checkertxt" & i.ToString), TextBox)
                On Error Resume Next
                If TextBox2.Lines(i).Contains(word) Then
                    If tempTextBox.Text.Contains(word) Then
                    Else
                        tempTextBox.Text = tempTextBox.Text + " " + TxtbValAfterCompar.Text()
                    End If
                Else
                End If
            End If
        Next
    Next

    Private Sub CompareNumbers()
        'First Textbox that is to be used for compare
        Dim textBox1Numbers As List(Of Integer) = GetNumbersFromTextLine(N1Check.Text)
        'Second Textbox that is to be used for compare
        Dim textBox2Numbers As List(Of Integer) = GetNumbersFromTextLine(TxtbValBeforeCompar.Text)
        'Union List of Common Numbers (this uses a lambda expression, it can be done using two For Each loops instead.)
        Dim commonNumbers As List(Of Integer) = textBox1Numbers.Where(Function(num) textBox2Numbers.Contains(num)).ToList()
        'This is purely for testing to see if it worked you can.
        Dim sb As StringBuilder = New StringBuilder()
        For Each foundNum As Integer In commonNumbers
            sb.Append(foundNum.ToString()).Append(" ")
            TxtbValAfterCompar.Text = (sb.ToString())
        Next
    End Sub
    Private Function GetNumbersFromTextLine(ByVal sTextLine As String) As List(Of Integer)
        Dim numberList As List(Of Integer) = New List(Of Integer)()
        Dim sSplitNumbers As String() = sTextLine.Split(" ")
        For Each sNumber As String In sSplitNumbers
            If IsNumeric(sNumber) Then
                Dim iNum As Integer = CInt(sNumber)
                TxtbValAfterCompar.Text = iNum
                If Not numberList.Contains(iNum) Then
                    TxtbValAfterCompar.Text = ("")
                    numberList.Add(iNum)
                End If
            Else
            End If
        Next
        Return numberList
    End Function

代码2:删除重复的字符(字符)

Module Module1



Function RemoveDuplicateChars(ByVal value As String) As String
    ' This table stores characters we have encountered.
    Dim table(value.Length) As Char
    Dim tableLength As Integer = 0

    ' This is our result.
    Dim result(value.Length) As Char
    Dim resultLength As Integer = 0

    For i As Integer = 0 To value.Length - 1
        Dim current As Char = value(i)
        Dim exists As Boolean = False

        ' Loop over all characters in the table of encountered chars.
        For y As Integer = 0 To tableLength - 1
            ' See if we have already encountered this character.
            If current = table(y) Then
                ' End the loop.
                exists = True
                y = tableLength
            End If
        Next

        ' If we have not encountered the character, add it.
        If exists = False Then
            ' Add character to the table of encountered characters.
            table(tableLength) = current
            tableLength += 1

            ' Add character to our result string.
            result(resultLength) = current
            resultLength += 1
        End If
    Next

    ' Return the unique character string.
    Return New String(result, 0, resultLength)
End Function

Sub Main()

    ' Test the method we wrote.
    Dim test As String = "having a good day"
    Dim result As String = RemoveDuplicateChars(test)
    Console.WriteLine(result)

    test = "areopagitica"
    result = RemoveDuplicateChars(test)
    Console.WriteLine(result)
End Sub
End Module

1 个答案:

答案 0 :(得分:2)

您可以使用一些LINQ,例如IntersectUnion

假设您的TextBox1TextBox2包含您提供的文本。

这是查找重复和不重复字符的简单方法。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim firstBoxList = TextBox1.Text.Split(",").ToList()
    Dim secondBoxList = TextBox2.Text.Split(",").ToList()

    Dim intersectionList = firstBoxList.Intersect(secondBoxList)

    For Each str As String In intersectionList
        TextBox3.Text = TextBox3.Text & str & ","
    Next

    TextBox4.Text = intersectionList.Count()

    Dim notRepeatedCharacter = firstBoxList.Union(secondBoxList).ToList
    notRepeatedCharacter.RemoveAll(Function(x) intersectionList.Contains(x))

    For each str As String In notRepeatedCharacter
        TextBox5.Text = TextBox5.Text & str & ","
    Next

    TextBox6.Text = notRepeatedCharacter.Count()
End Sub

输出是这样的:

Program Output

这考虑到两个文本框都不是重复字符。

如果您只想从第一个列表到第二个列表中找到不重复的字符,则应该这样做:

firstBoxList.RemoveAll(Function(x) secondBoxList.Contains(x))
For Each str As String In firstBoxList
    TextBox7.Text = TextBox7.Text & str & ","
Next

TextBox8.Text = firstBoxList.Count

这是输出:

second program output

下面是使用String.Join的完整代码,以使列表在文本框中看起来更平滑:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'First we grab all the numbers written inside the textboxes (I am not verifying anything)
    Dim firstBoxList = TextBox1.Text.Split(",").ToList()
    Dim secondBoxList = TextBox2.Text.Split(",").ToList()

    'Second we intersect the two lists and show them
    Dim intersectionList = firstBoxList.Intersect(secondBoxList)
    TextBox3.Text = String.Join(",", intersectionList)
    TextBox4.Text = intersectionList.Count()

    'We're checking the distintc character from both lists
    Dim notRepeatedCharacter = firstBoxList.Union(secondBoxList).ToList
    notRepeatedCharacter.RemoveAll(Function(x) intersectionList.Contains(x))
    TextBox5.Text = String.Join(",", notRepeatedCharacter)
    TextBox6.Text = notRepeatedCharacter.Count()

    'we're checkng the distinct character inside first list that doesn't show in second list
    firstBoxList.RemoveAll(Function(x) secondBoxList.Contains(x))
    TextBox7.Text = String.Join(",", firstBoxList)
    TextBox8.Text = firstBoxList.Count
End Sub