首页 > 解决方案 > 我可以重复使用表单来声明多个变量吗?

问题描述

我正在创建一个分数锦标赛系统。我创建了一个输入屏幕,用户可以在其中输入一个组的多个分数。当按下提交按钮时,我需要记录分数到分数,以便它们可以进入我的排行榜页面上的列表。

以下是我当前的代码。是否可以在用户每次选择提交时刷新表单,但也可以记录刷新之前表单的结果?如果没有,我担心我需要为每个组创建一个新表单。确定不是这样吗?

Public Class GT_Entry
    Dim Activityscore1 As Integer
    Dim Activityscore2 As Integer
    Dim Activityscore3 As Integer
    Dim Activityscore4 As Integer
    Dim Activityscore5 As Integer
    Dim Groupname As String
    Private Sub Submit_Click(sender As System.Object, e As System.EventArgs) Handles Submit.Click
        Activityscore1 = R1S.Text
        Activityscore2 = R2S.Text
        Activityscore3 = R3S.Text
        Activityscore4 = R4S.Text
        Activityscore5 = R5S.Text
        Groupname = GN.Text
        GN.Clear()
        R1S.Clear()
        R2S.Clear()
        R3S.Clear()
        R4S.Clear()
        R5S.Clear()
    End Sub

标签: vb.netvisual-studiolist

解决方案


有几种方法可以解决您的问题。我做了一个类来存储数据。然后创建了该类的列表。每次用户单击提交时,数据都会添加到列表中。您可以迭代列表并访问属性。

Private ScoreList As New List(Of GroupActivityScore)

Private Sub Submit_Click(sender As System.Object, e As System.EventArgs) Handles Submit.Click
    Dim GAS As New GroupActivityScore
    GAS.Score1 = CInt(R1S.Text)
    GAS.Score2 = CInt(R2S.Text)
    GAS.Score3 = CInt(R3S.Text)
    GAS.Score4 = CInt(R4S.Text)
    GAS.Score5 = CInt(R5S.Text)
    GAS.GroupName = GN.Text
    ScoreList.Add(GAS)
    GN.Clear()
    R1S.Clear()
    R2S.Clear()
    R3S.Clear()
    R4S.Clear()
    R5S.Clear()
End Sub

Public Class GroupActivityScore
    Public Property Score1 As Integer
    Public Property Score2 As Integer
    Public Property Score3 As Integer
    Public Property Score4 As Integer
    Public Property Score5 As Integer
    Public Property GroupName As String
End Class

推荐阅读