如何在类型列表中跟踪项目索引

时间:2015-04-08 16:16:00

标签: vb.net list

这可能是一个简单的问题,但我对它有一个真正的心理障碍。我在向前和向后浏览列表时试图在我的reportList(报告类型)中跟踪报告的索引。

我通过在组合框中指定所选项目的索引来完成这一方法。但是我还需要直接从列表中填写(已填充),用户的唯一输入是下一个和上一个按钮。

我在哪里设置报告:

    If wantFixture = True Then
                ind = UC_Menu_Scout1.cmbSelectedPlayer.SelectedIndex
            Else
                ind = reportList(0) 'index of item in reportList- 

'this doesn't work because reportList is of type Report, not integer  
            End If

reportList.Clear()
        reportList = retrieveReport()

        '*****General Information
        UC_Menu_Scout1.lblRptPosition.Text = reportList(ind).PositionPlayed

可以导航到下一个报告:

Private Sub btnNextPlayer_Click(sender As System.Object, e As System.EventArgs) Handles btnNextPlayer.Click
            'Moves to next item in playerList
            Dim x As Integer = cmbSelectedPlayer.SelectedIndex
            If x = cmbSelectedPlayer.Items.Count - 1 Then
                x = 0
            Else
                x += 1
            End If

            cmbSelectedPlayer.SelectedIndex = x

            lblNumberOfReports.Text = "Report: " & x + 1 & "/" & cmbSelectedPlayer.Items.Count

        End Sub

1 个答案:

答案 0 :(得分:0)

将当前索引和报告存储在变量中。

班级变量:

Private curIndex As Integer = 0
Private curReport As Report = Nothing

下一步按钮:

If Not curIndex = reportList.Count - 1 Then
  curIndex += 1
End If
curReport = reportList.ElementAt(curIndex)

上一个按钮:

 If Not curIndex = 0 Then
  curIndex -= 1
End If
curReport = reportList.ElementAt(curIndex)