在文本框中删除重复项并添加相应的值

时间:2018-07-27 12:18:45

标签: vb.net addition

我有一个带有三个TextBox的VB表单。这是我希望程序实现的示例:

所以,这就是表格...该程序对文本文件进行排序,并获取名称,目标和位置。例如

  • 约旦26中心
  • 詹姆斯10中旬
  • 乔丹4中心
  • 杰克6前进
  • 詹姆斯10中旬

单击更新按钮时,程序应意识到詹姆斯和乔丹被写过两次,删除其中之一并添加他们的目标,因此应输出:

  • 约旦30中心
  • 詹姆斯20中旬
  • 杰克6前进

为此,我将数据传输到ListBoxes中,这使得删除重复项更加容易,然后将数据传输回多行TextBox中,以便可编辑。到目前为止,这是我的代码。要么给出错误的结果,要么索引超出范围。

Dim Count1 As Integer
Dim Count2 As Integer
Dim Count3 As Integer
Dim NewInt As Integer
Dim ValOne As Integer
Dim ValTwo As Integer
ListBox1.Items.Clear()
ListBox2.Items.Clear()
ListBox3.Items.Clear()
NewInt = 0
ValOne = 0
ValTwo = 0
ListBox1.Items.AddRange(Players.Text.Split(vbNewLine))
ListBox2.Items.AddRange(Goals.Text.Split(vbNewLine))
ListBox3.Items.AddRange(Positions.Text.Split(vbNewLine))
Count1 = ListBox1.Items.Count
Count2 = ListBox2.Items.Count
Count3 = ListBox3.Items.Count
If Count1 = Count2 And Count1 = Count3 And Count2 = Count3 Then
    'Set two counters to compare all words with each other
    For iFirstCounter As Integer = 0 To ListBox1.Items.Count - 1
        For iSecondCounter As Integer = 0 To ListBox1.Items.Count - 1
            'Make sure there will not be an 'out of range' error,
            'because you are removing items from the listbox.
            iSecondCounter = Convert.ToInt64(iSecondCounter)
            iFirstCounter = Convert.ToInt64(iFirstCounter)
            ListBox2.Items.RemoveAt(iSecondCounter)
            ListBox2.Items.RemoveAt(iFirstCounter)
            If iFirstCounter < iSecondCounter Then
                ListBox2.Items.Insert(iFirstCounter, NewInt.ToString)
            Else
                ListBox2.Items.Insert(iSecondCounter, NewInt.ToString)
            End If
        Next
    Next
    Players.Text = ""
    Goals.Text = ""
    Positions.Text = ""
    Dim i As Integer
    For i = 0 To ListBox1.Items.Count - 1
        If Players.Text = "" Then
            Players.Text = ListBox1.Items(i)
        Else
            Players.Text = Players.Text & vbNewLine & ListBox1.Items(i)
        End If

    Next
    Dim a As Integer
    For a = 0 To ListBox2.Items.Count - 1
        If Goals.Text = "" Then
            Goals.Text = ListBox2.Items(a)
        Else
            Goals.Text = Goals.Text & vbNewLine & ListBox2.Items(a)
        End If
    Next
    Dim b As Integer
    For b = 0 To ListBox3.Items.Count - 1
        If Positions.Text = "" Then
            Positions.Text = ListBox3.Items(b)
        Else
            Positions.Text = Positions.Text & vbNewLine & ListBox3.Items(b)
        End If
    Next
Else
    MessageBox.Show("The Text Boxes don't contain an equal number of values ... please add more/remove some values")
End If

3 个答案:

答案 0 :(得分:0)

可以通过多种方式完成,例如:

 If TextBox2.Lines.Count > 1 Then
        Dim LineList As List(Of String) = TextBox2.Lines.ToList 'textbox lines
        Dim NewLines As List(Of String) = TextBox2.Lines.ToList 'can't edit list we're looping over, a copy of lines

        Dim NamesList As New List(Of String)
        For x = 0 To LineList.Count - 1
            Dim linesplit As String() = LineList(x).Split({" "}, StringSplitOptions.RemoveEmptyEntries)
            If NamesList.Contains(linesplit(0)) Then
                NewLines.Remove(LineList(x))
            Else
                NamesList.Add(linesplit(0))
            End If
        Next
        TextBox2.Lines = NewLines.ToArray
    End If

enter image description here

答案 1 :(得分:0)

这是一个通过LINQ和Lambdas完成此操作的代码示例。

Module Module1

    Sub Main()



        Dim ungroupedPlayers(1) As String
        ungroupedPlayers(0) = "Jordan 26 Center"
        ungroupedPlayers(1) = "Jordan 4 Center"

        Dim players = ungroupedPlayers.ToList().ConvertAll(Of Player)(Function(x As String) As Player
                                                                          Dim split() As String = x.Split(" "c)
                                                                          Dim p As New Player
                                                                          p.PlayerName = split(0)
                                                                          p.Count = split(1)
                                                                          p.Position = split(2)
                                                                          Return p
                                                                      End Function)
        Dim playersGrouped = From p In players
                             Group By PlayerName = p.PlayerName Into g = Group
                             Select PlayerName, Count = g.Sum(Function(ip As Player) ip.Count), Position = g.Min(Function(ip As Player) ip.Position.ToString())

        Dim groupedPlayers() As String = playersGrouped.ToList().ConvertAll(Of String)(Function(ip)
                                                                                           Return ip.PlayerName.ToString() & " " & ip.Count.ToString() & " " & ip.Position.ToString()
                                                                                       End Function).ToArray()
         For Each groupedPlayer as String in groupedPlayers
             Console.WriteLine(groupedPlayer)
         Next
         Console.Read()
    End Sub

    Public Class Player
        Public PlayerName As String
        Public Count As Integer
        Public Position As String
    End Class

End Module

答案 2 :(得分:0)

您不需要繁琐的ListBox控制即可处理玩家数据。
使用List(Of T)并创建类Player可以提高可读性。

您可以删除重复项,然后才能在表单中显示值。
除了使用多行文本框之外,您还可以使用DataGridView作为“编辑数据的正确工具”。

Public Class Player
    Public Property Name As String
    Public Property Position As String
    Public Property Goals As Integer
End

Public Class PlayersForm : Form
    Private Sub Form_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load
        Dim data As List(Of Player) = LoadPlayersData()
        Dim players As List(Of Player) = NormalizeData(data)

        ' Use DataGridView
        Me.DataGridView1.DataSource = players                                                
    End Sub

    Private Function LoadPlayersData() As List(Of Player)
        Dim rawData As String() = File.ReadAllLines("pathToTextFile")
        Return rawData.Select(Function(line) LineToPlayer(line)).ToList()
    End Function

    Private Function NormalizeData(players As List(Of Player)) As List(Of Player)
        Return players.Group(Function(player) player.Name)
                      .Select(Function(group)
                                  Return New Player With
                                  {
                                      .Name = group.Key,
                                      .Position = group.First().Position,
                                      .Goals = group.Sum(Function(player) player.Goals)
                                  }
                              End Function)
                      .ToList()
    End Function

    Private Function LineToPlayer(line As String) As Player
        Dim values = line.Split(" "c)
        Return New Player With
        {
            .Name = values(0),
            .Position = values(2),
            .Goals = Integer.Parse(values(1))
        }
    End Function
End Class 

DataGridView控件将在您进行任何更改时自动更新您的玩家名单。这样您就可以使用其他一些控件来自动显示最佳得分手,例如,而无需额外地将数据从字符串转换为整数再返回。