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

问题描述 投票:0回答:3

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

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

  • 约旦26中心
  • 詹姆斯10中
  • 约旦4中心
  • 杰克6前锋
  • 詹姆斯10中

当点击更新按钮时,程序应该意识到James和Jordan被写了两次,删除其中一个并添加他们的目标,所以它应该输出:

  • 乔丹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
vb.net addition
3个回答
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


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

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控件将自动更新您的列表(播放器)。这使你有可能有一些其他控件自动显示最好的得分手,例如,没有额外的数据从字符串转换为整数和返回。

© www.soinside.com 2019 - 2024. All rights reserved.