2014-04-03 27 views
0

将测试分数存储并写入与其他用户的分数并存储在变量中的文本文件。如何在关闭程序后保持价值? VB.net

该计划内是一个评分表格,显示该进度表格中该用户所有测试的分数。它在我完成测试后正常工作,但是当我注销并关闭程序时,它会在标签中返回到0。

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

我该怎么做,以便在我为该用户运行程序时保持相同的值?

末子模块创建存储公共变量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 =假 昏暗的FileFound由于布尔 的FileFound =假 的FileOpen(1,Filenamet,OpenMode.Input) 尽管不EOF(1 )和的FileFound =假 输入(1名) 输入(1 '所有的细节都从TeacherAccounts文本文件阅读该帐户',密码) 输入(1,namet) 输入(1,surnamet)

 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` 

进展形式码... 如果yearst = “12” 和classst = “A” 然后 昏暗的FileFound由于布尔 的FileFound =假 的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是一个字符串,并设置为模块中的公共变量。

+0

请向我们显示您的代码。没有办法猜测发生了什么。 –

+0

我已重新编辑代码 – user3167227

+0

内容阅读的代码是相同的 - 我建议您更改“case”或“if”中的文件名,但使用相同的阅读代码。 – evenro

回答

4

有你3种不同的选择:

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

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

  3. 使用用户区域app.config来存储这些数据。
    这个区域与应用程序区域相矛盾,可以由应用程序修改,并且会分别保留给每个用户。

我觉得第三个是最简单和最快的。
好运


所以我觉得问题是,在该文件中读取你不“缩小”相关的用户。

在此代码:

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 

这应该解决的问题。

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

+0

发表涉及变量Topic1Score – user3167227

+0

的代码添加了阅读部分。 – user3167227

+0

你在每个文件中只有一个用户吗? – evenro

相关问题