【问题标题】:How to modify a value in the array in visual basic如何在visual basic中修改数组中的值
【发布时间】:2012-10-02 07:13:07
【问题描述】:

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

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

结束类**

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

【问题讨论】:

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


    【解决方案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 类以包含您需要存储的任何内容。与字典相反,列表也允许您使用索引值。

    【讨论】:

    • 我忘了说,编辑本身就是一个新按钮。所以你认为我仍然可以在那里使用字典。我上面粘贴的代码是在数组中输入值。现在我想更改任何值,然后单击编辑按钮以更改数组中的这些值。
    • 当然,如果您将字典引用放在类级别,则可以从同一类中的任何按钮引用它。
    • 好的,让我们看看 - 究竟是什么不起作用?您收到错误消息吗? (请张贴)。我将编辑我的答案以使其更清楚
    • Senthil,我在原始答案中看到我留下了“学生”值而不是 txtStudent.Text。如果您按原样使用代码,请替换。我已经更新了原始示例。
    • 对于 redim 语句,它给我一个错误“Redim 需要一个数组。”同样对于 txtStudent.Text 它说的参数太多
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 2021-06-23
    相关资源
    最近更新 更多