如何在visual basic中修改数组中的值

时间:2012-10-02 07:13:07

标签: arrays vb.net visual-studio-2010

以下是我用于创建二维数组的代码

Public Class frmGrades
Private Score As Dictionary(Of String, String) = New Dictionary(Of String, String)

Private Sub cmdApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdApply.Click
    Static intNumber As Integer
    ReDim Preserve Score(1, intNumber)
    Score(0, intNumber) = txtStudent.Text
    Score(1, intNumber) = txtGrade.Text
    hsbStudent.Maximum = intNumber
    hsbStudent.Value = intNumber
    intNumber = intNumber + 1
    txtGrade.Clear()
    txtStudent.Clear()
    txtStudent.Focus()

End Sub

Private Sub hsbStudent_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles hsbStudent.Scroll
    txtStudent.Text = Score(0, hsbStudent.Value)
    txtGrade.Text = Score(1, hsbStudent.Value)

End Sub

Private Sub cmdFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFirst.Click
    txtStudent.Text = Score(0, hsbStudent.Minimum)
    txtGrade.Text = Score(1, hsbStudent.Minimum)

End Sub

Private Sub cmdLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLast.Click
    txtStudent.Text = Score(0, hsbStudent.Maximum)
    txtGrade.Text = Score(1, hsbStudent.Maximum)
End Sub

**Private Sub CmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdEdit.Click

    If Score.ContainsKey(txtStudent.Text) Then
        Score.Item(txtStudent.Text) = txtGrade.Text
    Else
        Score.Add(txtStudent.Text, txtGrade.Text)
    End If
End Sub

结束班**

现在我想编辑等级文本框,它也应该改变数组中的等级。关于如何做到这一点的任何想法。

1 个答案:

答案 0 :(得分:1)

更新:

好的,我现在看到代码有了更多,问题更加清晰,需要采用不同的方法。我重写了你的代码。看看这是否适合你:

Public Class frmGrades

    Public Class StudentGrade
        Public Name As String
        Public Grade As String
    End Class

    Private Score As List(Of StudentGrade) = New List(Of StudentGrade)

    Private Sub cmdApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdApply.Click

        AddStudent()

    End Sub

    Private Sub hsbStudent_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles hsbStudent.Scroll
        Update(hsbStudent.Value - 1) 'lists are zero-based
    End Sub

    Private Sub cmdFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFirst.Click
        Update(0)
    End Sub

    Private Sub cmdLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLast.Click
        Update(hsbStudent.Maximum - 1)
    End Sub
    Private Sub AddStudent()
        Dim nm As New StudentGrade
        nm.Name = txtStudent.Text
        nm.Grade = txtGrade.Text
        Score.Add(nm)

        hsbStudent.Minimum = 1
        hsbStudent.Maximum = Score.Count 
        hsbStudent.Value = Score.Count 

        txtGrade.Clear()
        txtStudent.Clear()
        txtStudent.Focus()
    End Sub
    Private Sub Update(i As Integer)
        txtStudent.Text = Score(i).Name
        txtGrade.Text = Score(i).Grade
    End Sub
    Private Sub CmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdEdit.Click

        Dim index As Integer = -1, cnt As Integer = 0

        For Each nm As StudentGrade In Score
            If nm.Name.ToLower = txtStudent.Text.ToLower Then
                index = cnt
                Exit For
            End If
            cnt += 1
        Next

        If index = -1 Then
            AddStudent()
        Else
            Score(index).Name = txtStudent.Text
            Score(index).Grade = txtGrade.Text
            hsbStudent.Value = index + 1
        End If

    End Sub

End Class

使用此代码,您可以扩展StudentGrade类以包含您需要存储的任何内容。与字典相反,该列表允许您使用索引值。