关闭程序后如何保持价值不变? VB.net

时间:2014-04-03 13:49:28

标签: vb.net

将测试分数存储并写入包含其他用户分数的文本文件并存储在变量中。

在程序中是一个分数表单,在“进度”表单中显示该用户的所有测试的分数。它在我完成测试后工作,但是当我退出并关闭程序时,它会回到标签中的0。

然而,当我关闭程序然后重新运行程序时,值会变回0而不是写入文本文件的实际测试分数。

当我为该用户运行程序时,我如何才能使值保持不变?

创建了End Sub Module以存储公共变量Public Topic1Score As Integer

这是测试本身的代码摘录

If answers(i) = questions(i, 4) And FileOpenStatusTS = False Then
    Topic1Score += 1
    TotalScore += 1
End If
Next

If yearst = "12" And classst = "A" Then
    FileOpen(1, FileName12A1, OpenMode.Append)
    FileOpenStatus12A1 = True`

输入并检查所有详细信息后,它们将被写入教师帐户文本文件

WriteLine(1, Username, Topic1Score, TotalScore)
FileClose(1)
End If

以下是进度表单中文件的读取。

Private Sub StProgress_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Debug.Assert(Not String.IsNullOrWhiteSpace(Topic1Score))
    lblTotalScore.Text = TotalScore

    LblTopic2Score.Text = "You scored " & Topic2Score & " out of 5"

    lblStName.Text = namest
    LblStSurname.Text = surnamest


    If yearst = "12" And classst = "A" Then
        Dim Filefound As Boolean
        Filefound = False
        FileOpen(1, FileName12A1, OpenMode.Input)
        While Not EOF(1) And Filefound = False


            Input(1, Username) 'All the details are read from that account from the 12A1 text file'
            Input(1, Topic1Score)
            Input(1, TotalScore)
            Filefound = True
            lblTopic1Score.Text = "You scored " & Topic1Score & " out of 5"
        End While
    End If

    If yearst = "13" And classst = "A" Then
        Dim Filefound As Boolean
        Filefound = False
        FileOpen(1, FileName13A1, OpenMode.Input)
        While Not EOF(1) And Filefound = False


            Input(1, Username) 'All the details are read from that account from the 12A1 text file'
            Input(1, Topic1Score)
            Input(1, TotalScore)
            Filefound = True
            lblTopic1Score.Text = "You scored " & Topic1Score & " out of 5"
        End While
    End If
    If yearst = "12" And classst = "B" Then
        Dim Filefound As Boolean
        Filefound = False
        FileOpen(1, FileName12B1, OpenMode.Input)
        While Not EOF(1) And Filefound = False
            Input(1, Username) 'All the details are read from that account from the 12A1 text file'
            Input(1, Topic1Score)
            Input(1, TotalScore)
            Filefound = True
            lblTopic1Score.Text = "You scored " & Topic1Score & " out of 5"
        End While
    End If
    If yearst = "13" And classst = "B" Then
        Dim Filefound As Boolean
        Filefound = False
        FileOpen(1, FileName13B1, OpenMode.Input)
        While Not EOF(1) And Filefound = False


            Input(1, Username) 'All the details are read from that account from the 12A1 text file'
            Input(1, Topic1Score)
            Input(1, TotalScore)
            Filefound = True
            lblTopic1Score.Text = "You scored " & Topic1Score & " out of 5"
        End While
    End If
End Sub`

最新代码...... 测试中的代码...... `FileOpenStatusT = False         Dim Filefound As Boolean         Filefound = False         FileOpen(1,Filenamet,OpenMode.Input)         而不是EOF(1)和Filefound = False             输入(1,用户名)'从TeacherAccounts文本文件中读取该帐户的所有详细信息'             输入(1,密码)             输入(1,namet)             输入(1,姓氏)

        If Username = TxtUsername.Text And Password = TxtPassword.Text Then 'If the username and account entered are valid then the user is navigated to the TeacherMenu form'
            Filefound = True
            t = Username
        End If
    End While

    If Filefound = False Then
        MsgBox("Username and Password were not a match,please try again")
    Else
        TeacherMenu.Show()
        Me.Hide()
    End If`

进度表格代码......  如果年龄=" 12"而classst =" A"然后             Dim Filefound As Boolean             Filefound = False             FileOpen(1,FileName12A1,OpenMode.Input)             虽然不是EOF(1)和Filefound = False

            Input(1, Username) 'All the details are read from that account from the 12A1 text file'
            Input(1, Topic1Score)
            Input(1, TotalScore)
            Filefound = True
            If Username = t Then
                lblTopic1Score.Text = "You scored " & Topic1Score & " out of 5"
            End If

t是一个字符串,并在模块中设置为公共变量。

1 个答案:

答案 0 :(得分:4)

您有3种不同的选择:

  1. 使用文件系统,并为其创建一个文件(需要一些工作) 如果我理解正确 - 这就是你所做的,而且你有一个错误......为获得调试帮助 - 发布代码。
    我认为您的代码中的问题是您在追加模式下打开文件 你没有发布文件的读取,但我猜你在那里读了第一个分数,但不是每次都写它 - 你写在文件的末尾。
    所以你的文件看起来可能是这样的:
    0
    1
    2
    你总是读0 ...
    如果这不是问题 - 也可以发布阅读。

  2. 使用注册表来保存变量(需要的工作量较少)。

  3. 使用用户区app.config存储此数据 该区域与应用程序区域相矛盾,可以由应用程序修改,并将为每个用户单独保留。

  4. 我认为第三种是最简单最快的 祝你好运


    所以我认为问题是在文件读取中你没有“缩小”相关用户。

    在此代码中:

       Dim Filefound As Boolean
        Filefound = False
        FileOpen(1, FileName12B1, OpenMode.Input)
        While Not EOF(1) And Filefound = False
            Input(1, Username) 'All the details are read from that account from the 12A1 text file'
            Input(1, Topic1Score)
            Input(1, TotalScore)
            Filefound = True
    -------IF THE Username == t here... - the currently logged in user ... run the following line
            lblTopic1Score.Text = "You scored " & Topic1Score & " out of 5"
    -------IF the Username == the currently logged in user - break the loop
    ------- UNLESS - the last student record is necessary, since the grades were written in append mode
    
        End While
    

    这应该可以解决问题。

    是练习系统还是“真实”系统?
    我问,因为允许从终端客户端访问保存用户数据的文件是不安全的...实践它是正常的,但对于真正的系统,您需要更改架构。